仅执行最后一行的AWK中的系统(“系统”)调用

仅执行最后一行的AWK中的系统(“系统”)调用,awk,Awk,我已经编写了一个awk脚本,它从file.txt中获取一些值,并为此文件的每一行调用system(“…”)。 问题是终端只接受最后一次系统调用 我需要为我的文件.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

我已经编写了一个awk脚本,它从file.txt中获取一些值,并为此文件的每一行调用
system(“…”)
。 问题是终端只接受最后一次系统调用

我需要为我的
文件.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
}

无论如何,您每行调用一次外部进程,因此使用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
读取-r交换机mac ip dst输出端口时;做
#带有“$switch\u mac”、“$ip\u dst”和“$output\u port”的curl命令(包括引号!)
完成

如果您愿意,您可以使用
output=$(curl…
捕获响应,或者如果您愿意,甚至可以将循环的输出通过管道传输到awk。

您可以简单地使用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地址,使其在我的网络上工作。

为什么要在
awk
中使用此地址?您要运行的实际
curl
命令是什么?您能否给出一个示例,而不使用所有可怕的引号,因为在awk中使用了
awk
,将两个字符串相邻放置是字符串串联。因此,不需要所有这些引用;他们什么也不做,只是把代码弄得乱七八糟。