I am trying to run a shell command from within awk for each line of a file, and the shell command needs one input argument. I tried to use system()
, but it didn't recognize the input argument.
我试图在awk中为文件的每一行运行一个shell命令,而shell命令需要一个输入参数。我试图使用system(),但它没有识别输入参数。
Each line of this file is an address of a file, and I want to run a command to process that file. So, for a simple example I want to use 'wc' command for each line and pass $1
to wc.
该文件的每一行都是一个文件的地址,我想运行一个命令来处理该文件。因此,对于一个简单的例子,我想对每一行使用'wc'命令并将$ 1传递给wc。
awk '{system("wc $1")}' myfile
4 个解决方案
#1
53
you are close. you have to concatenate the command line with awk variables:
你很亲密你必须连接命令行与awk变量:
awk '{system("wc "$1)}' myfile
#2
34
You cannot grab the output of an awk system()
call, you can only get the exit status. Use the getline/pipe or getline/variable/pipe constructs
你无法获取awk system()调用的输出,你只能获得退出状态。使用getline / pipe或getline / variable / pipe结构
awk '{
cmd = "your_command " $1
while (cmd | getline line) {
do_something_with(line)
}
close(cmd)
}' file
#3
2
FYI here's how to use awk to process files whose names are stored in a file (providing wc-like functionality in this example):
仅供参考,如何使用awk处理其名称存储在文件中的文件(在此示例中提供类似wc的功能):
gawk '
NR==FNR { ARGV[ARGC++]=$0; next }
{ nW+=NF; nC+=(length($0) + 1) }
ENDFILE { print FILENAME, FNR, nW, nC; nW=nC=0 }
' file
The above uses GNU awk for ENDFILE. With other awks just store the values in an array and print in a loop in the END section.
以上使用GNU awk进行ENDFILE。使用其他awks只需将值存储在数组中,然后在END部分的循环中打印。
#4
0
Or use the pipe |
as in bash then retrive the output in a variable with awk's getline
, like this
或者使用管道就像在bash中那样用awk的getline在变量中检索输出,就像这样
zcat /var/log/fail2ban.log* | gawk '/.*Ban.*/ {print $7};' | sort | uniq -c | sort | gawk '{ "geoiplookup " $2 "| cut -f2 -d: " | getline geoip; print $2 "\t\t" $1 " " geoip}'
That line will print all the banned IPs from your server along with their origin (country) using the geoip-bin package.
该行将使用geoip-bin包从您的服务器打印所有被禁止的IP及其来源(国家/地区)。
The last part of that one-liner is the one that affects us :
单线的最后一部分是影响我们的部分:
gawk '{ "geoiplookup " $2 "| cut -f2 -d: " | getline geoip; print $2 "\t\t" $1 " " geoip}'
It simply says : run the command "geoiplookup 182.193.192.4 | -f2 -d:"
($2 gets substituted as you may guess) and put the result of that command in geoip (the | getline geoip
bit). Next, print something something and anything inside the geoip
variable.
它简单地说:运行命令“geoiplookup 182.193.192.4 | -f2 -d:”($ 2代替你猜测)并将该命令的结果放在geoip(| getline geoip位)中。接下来,在geoip变量中打印一些东西和任何东西。
The complete example and the results can be found here, an article I wrote.
我写的这篇文章可以在这里找到完整的例子和结果。
#1
53
you are close. you have to concatenate the command line with awk variables:
你很亲密你必须连接命令行与awk变量:
awk '{system("wc "$1)}' myfile
#2
34
You cannot grab the output of an awk system()
call, you can only get the exit status. Use the getline/pipe or getline/variable/pipe constructs
你无法获取awk system()调用的输出,你只能获得退出状态。使用getline / pipe或getline / variable / pipe结构
awk '{
cmd = "your_command " $1
while (cmd | getline line) {
do_something_with(line)
}
close(cmd)
}' file
#3
2
FYI here's how to use awk to process files whose names are stored in a file (providing wc-like functionality in this example):
仅供参考,如何使用awk处理其名称存储在文件中的文件(在此示例中提供类似wc的功能):
gawk '
NR==FNR { ARGV[ARGC++]=$0; next }
{ nW+=NF; nC+=(length($0) + 1) }
ENDFILE { print FILENAME, FNR, nW, nC; nW=nC=0 }
' file
The above uses GNU awk for ENDFILE. With other awks just store the values in an array and print in a loop in the END section.
以上使用GNU awk进行ENDFILE。使用其他awks只需将值存储在数组中,然后在END部分的循环中打印。
#4
0
Or use the pipe |
as in bash then retrive the output in a variable with awk's getline
, like this
或者使用管道就像在bash中那样用awk的getline在变量中检索输出,就像这样
zcat /var/log/fail2ban.log* | gawk '/.*Ban.*/ {print $7};' | sort | uniq -c | sort | gawk '{ "geoiplookup " $2 "| cut -f2 -d: " | getline geoip; print $2 "\t\t" $1 " " geoip}'
That line will print all the banned IPs from your server along with their origin (country) using the geoip-bin package.
该行将使用geoip-bin包从您的服务器打印所有被禁止的IP及其来源(国家/地区)。
The last part of that one-liner is the one that affects us :
单线的最后一部分是影响我们的部分:
gawk '{ "geoiplookup " $2 "| cut -f2 -d: " | getline geoip; print $2 "\t\t" $1 " " geoip}'
It simply says : run the command "geoiplookup 182.193.192.4 | -f2 -d:"
($2 gets substituted as you may guess) and put the result of that command in geoip (the | getline geoip
bit). Next, print something something and anything inside the geoip
variable.
它简单地说:运行命令“geoiplookup 182.193.192.4 | -f2 -d:”($ 2代替你猜测)并将该命令的结果放在geoip(| getline geoip位)中。接下来,在geoip变量中打印一些东西和任何东西。
The complete example and the results can be found here, an article I wrote.
我写的这篇文章可以在这里找到完整的例子和结果。