linux - 在 linux 命令行中的一个命令中多次使用 xargs

标签 linux bash shell sed

我正在尝试写一个单行,但无法弄清楚我做错了什么。我正在尝试使用以下命令:

cat testadds | cut -f 1 -d "," | xargs -ifoo /bin/bash -c "cat testadds | cut -f 2 -d \",\" | xargs --replace=addr /bin/bash -c \"cat testadds | cut -f 3 -d \",\" | xargs --replace=num /bin/bash -c \"cat testmdl | sed 's/DUMMY/foo/g' | sed 's/IP1/addr/g' | sed 's/IP2/num/g'\"\""

我没有得到任何输出,我的 testadds 文件设置如下:

dev,IP1,IP2

当我只使用 2 个 xargs 时,它工作正常,但是当我添加第 3 个也是最后一个 xargs 时,它没有提供任何输出。我想知道在对文件进行分类时,您可以使用 xargs 的次数是否有限制。

我想预期的输入来自具有多个设备的文件。输入将是 testdevice,1.1.1.1,2.2.2.2

预期输出为:
-deviceSystemSoftware 'device:testdevice' '6500 7-SLOT OPTICAL SW:1021'
-deviceCname 'device:testdevice' 'PRIORITY SLA - identifier - testdevice'
-deviceDateAdded 'device:testdevice' '2017-02-24'
-deviceNotes 'device:testdevice' 'BTWB100269 - testdevice'
-hier 'nib:opr|0 group:Openreach group:TSO'
-hier 'nib:opr|0 group:Openreach group:TSO group:Ciena'
-hierUnique 'nib:opr|0 group:Openreach group:TSO group:Ciena device:testdevice'

-createEntity 'service:snmp-trap-handling{device:testdevice}CA|0[+opr-ciena-6500-alarms|+Nocol]'
-createEntity 'service:configuration-tracking{device:testdevice}opr|0[ciena6500]'

-createEntity 'interface:testdevice|COLAN-1-X'
-entityDescription 'interface:testdevice|COLAN-1-X' 'COLAN-1-X'
-createEntity '地址:测试设备|COLAN-1-X|1.1.1.1'
-devicePrimaryInterface 'device:testdevice' 'interface:testdevice|COLAN-1-X'
-deleteEntity 'address:testdevice|mgmt|1.1.1.1'
-deleteEntity 'service:ippingmon{interface:testdevice|mgmt}opr|0[]'
-deleteEntity 'interface:testdevice|mgmt'
-createEntity '接口(interface):测试设备|SHELFIP'
-entityDescription 'interface:testdevice|SHELFIP' 'SHELFIP'
-createEntity '地址:测试设备|SHELFIP|2.2.2.2'

希望对你有帮助

我想要完成的是修改文件以将它们显示为预期的输出。这是为了将它添加到我的监控系统中。抱歉,这是我第一次这样做,所以对于信息不足,我深表歉意。

最佳答案

您只需要一个 while 循环,即使在一行上也比您尝试的要短(而且便宜,因为没有启动外部程序;一切由内置命令完成):

# while IFS=, read -r dev ip1 ip2; do printf "-createEntity 'address:%s|%s|%s'\n" "$dev" COLAN-1-X "$ip1" "$dev" SHELFIP "$ip2"; done < input.txt
while IFS=, read -r dev ip1 ip2; do
  printf "-createEntity 'address:%s|%s|%s'\n" \
    "$dev" COLAN-1-X "$ip1" \
    "$dev" SHELFIP "$ip2"
done < input.txt

关于linux - 在 linux 命令行中的一个命令中多次使用 xargs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42424930/

相关文章:

linux - ImageMagick 使用转换和重命名输出

java - 使用 Java 代码从 Windows 机器运行 bash 脚本

python - 确定网站上某些文本的来源

php - ssh 连接虽然 ssh2_connect 失败

python - 在 python 或 bash 循环中运行 linux cat 命令

linux - 如果不在/etc/passwd 中,如何更改 Ubuntu 中的默认 shell?

linux - 在具有相似文件的目录中进行尾部操作

python - 执行任务后在 Python 中实现自动保存功能

linux - 创建一个文件夹,然后在同一脚本中将文件移动到其中

bash - 如果前 3 个八位字节匹配,如何使用 sed 替换文件中的所有 ip