system(“”)在AWK中调用仅执行最后一行

时间:2016-02-05 14:49:10

标签: awk

我编写了一个awk脚本,它从file.txt中获取一些值,并且对于该文件的每一行调用system("...")。 问题是终端只接受最后一次系统调用。

我需要为system("...")的每一行调用每个file.txt调用。

例如:file.txt

00:00:6c:19:8f:b1:d8:27 10.0.0.10 1 
00:00:6c:19:8f:b1:d8:27 10.0.0.11 1 
00:00:6c:19:8f:b1:d8:28 10.0.0.12 3
00:00:6c:19:8f:b1:d8:28 10.0.0.11 2

例如file.awk:

BEGIN {

}
{
switch_mac=$1
ip_dst=$2
output_port=$3

system("curl" " -d" " '{" "\"switch\""":" "\""switch_mac"\"""," "\"cookie\""":""\"1\"""," "\"priority\""":""\"2\"""," "\"eth_type\""":""\"0x0800\"""," "\"ipv4_dst\""":""\""ip_dst"\"""," "\"active\""":""\"true\"""," "\"actions\""":""\"output="output_port"\"""}'" " http://10.0.0.11:8080/wm/staticflowpusher/json")

//here i need to have the execution of the system call
}

2 个答案:

答案 0 :(得分:1)

无论如何,您每个线路都要调用一次外部进程,因此使用awk读取文件时可能会看到的任何加速都不值得担心:

while read -r switch_mac ip_dst output_port; do
    # your curl command with "$switch_mac", "$ip_dst" and "$output_port" (include the quotes!)
done < file.txt

如果您愿意,可以使用output=$(curl ...)捕获响应,或者如果需要,甚至可以将循环的输出传递给awk。

答案 1 :(得分:0)

你可以在这些方面与 GNU Parallel 并行地完成所有这些工作:

parallel --colsep ' ' -a file.txt echo curl -d '{"switch": "{1}", "cookie":"1", "priority":"2", "ip4_dst":"{2}", "active":"true", "actions":"output={3}"}' http://192.168.0.1:8080/wm/staticflowpusher/json

<强>输出

curl -d {switch: 00:00:6c:19:8f:b1:d8:27, cookie:1, priority:2, ip4_dst:10.0.0.10, active:true, actions:output=1} http://192.168.0.1:8080/wm/staticflowpusher/json
curl -d {switch: 00:00:6c:19:8f:b1:d8:27, cookie:1, priority:2, ip4_dst:10.0.0.11, active:true, actions:output=1} http://192.168.0.1:8080/wm/staticflowpusher/json
curl -d {switch: 00:00:6c:19:8f:b1:d8:28, cookie:1, priority:2, ip4_dst:10.0.0.12, active:true, actions:output=3} http://192.168.0.1:8080/wm/staticflowpusher/json
curl -d {switch: 00:00:6c:19:8f:b1:d8:28, cookie:1, priority:2, ip4_dst:10.0.0.11, active:true, actions:output=2} http://192.168.0.1:8080/wm/staticflowpusher/json

如果输出看起来正确,则从命令中删除单词echo,然后它将实际运行命令而不是回显它们。请注意,echo目前隐藏了一些双引号。另请注意,我更改了最终的IP地址,因此它可以在我的网络上运行。