我在 TCK/TK unix 命令中使用 AWK 命令时遇到问题:
var=`awk -v var1="^$line" -F "|" '$1~var1{print $1 }' sort-address-name-ip.txt | awk -v var2="$line$" '$0~var2'`
我用作:
set var5 [exec /usr/bin/awk -v var1={^$line} -F {|} {$1~var1{print $1 }} sort-address-name-ip.txt | /usr/bin/awk -v var2={$line$} {$0~var2}]
我的脚本:
#!/usr/bin/expect
set file [ open /home/gcngov/script/practice/tcl/policy-unique_single_line-src-dst-ip.txt]
while {[gets $file line] != -1} {
puts $line
set file1 [open /home/gcngov/script/practice/tcl/sort-address-name-ip.txt]
set var5 [exec /usr/bin/awk -v var1={^$line} -F {|} {$1~var1{print $1 }} sort-address-name-ip.txt | /usr/bin/awk -v var2={$line$} {$0~var2}]
puts $var5
}
close $file
输出:
~/script/practice/tcl$ expect -d while-file.sh
expect version 5.45
argv[0] = expect argv[1] = -d argv[2] = while-file.sh
set argc 0
set argv0 "while-file.sh"
set argv ""
executing commands from command file while-file.sh
10.13.210.212
missing close-bracket
while executing
"set var5 ["
("while" body line 5)
invoked from within
"while {[gets $file line] != -1} {
puts $line
#set var [exec /usr/bin/awk -v var1="^$line" -F "|" $1~var1{print $1 } sort-address-name-ip.txt | awk..."
(file "while-file.sh" line 3)
答案1
在 Tcl 中,大括号就像 shell 的单引号:不执行变量替换。你需要改变
var1={^$line}
# to
var1="^$line"
对于第二个 awk 也是如此。
但是,我不明白你为什么在管道中使用 2 个 awks:看起来你想找到policy-unique_single_line-src-dst-ip.txt
出现在管道分隔的第 1 列中的 IP sort-address-name-ip.txt
:
set ips [exec cut {-d|} -f1 sort-address-name-ip.txt | grep -Fxf policy-unique_single_line-src-dst-ip.txt]