【发布时间】:2015-07-01 08:11:54
【问题描述】:
我在向 AWK 添加变量时遇到了问题。
我有一个包含以下输入的文件:
MainComputer>
Device ID: name1
Interface: 1
random text...
Device ID: name2
Interface: 2
random text....
Device ID: name3
Interface: 3
random text....
现在我想打印所有变量:我已经拥有的:
#!/bin/bash
line=$(head -n 1 file)
var=$(echo $line | cut -d ">" -f1)
var2=$(awk '/Interface/ {print $2}' file)
awk -v var3="$var" '/Device/ {print var3, "->", $2, "[Label: "$var2"]}' file
但是 $var2 没有显示输出,如果我输入: var2 它会给出错误。
我想要的输出:
MainComputer -> name1 [Label: 1]
MainComputer -> name2 [Label: 2]
MainComputer -> name3 [Label: 3]
对于其他 40 个输入,依此类推......
但它只给出 MainComputer -> name1 和接口标签上的错误...
所以我正在寻找一种方法来打印我已经初始化的多个 var。
还是谢谢!
【问题讨论】:
-
您要查找的输出是什么?你能编辑你的问题给我们看吗?
-
很明显,为什么你正确分配了
-v var3="$var",然后却试图在里面使用$var2。 Shell 变量不要用单引号展开,你的整个 awk 命令都用单引号括起来。 -
就是你想要的那一行吗?您不想为
name2和name3显示任何内容吗?
标签: bash variables printing awk