简体   繁体   English

awk如何将unix命令的结果作为参数?

[英]how awk takes the result of a unix command as a parameter?

Say there is an input file with tabs delimited field, the first field is integer 假设存在一个带有制表符分隔字段的输入文件,第一个字段是整数

1 abc
1 def
1 ghi
1 lalala
1 heyhey
2 ahb
2 bbh
3 chch
3 chchch
3 oiohho
3 nonon
3 halal
3 whatever

First, i need to compute the counts of the unique values in the first field, that will be: 首先,我需要计算第一个字段中唯一值的计数,即:

5 for 1, 2 for 2, and 6 for 3

Then I need to find the max of these counts, in this case, it's 6. 然后我需要找到这些计数的最大值,在这种情况下为6。

Now i need to pass "6" to another awk script as a parmeter. 现在,我需要将“ 6”传递给另一个awk脚本作为参数。

I know i can use command below to get a list of count: 我知道我可以使用下面的命令来获取计数列表:

cut -f1 input.txt | sort | uniq -c | awk -F ' ' '{print $1}' | sort 

but how do i get the first count number and pass it to the next awk command as a parameter not as an input file? 但是我如何获取第一个计数数字并将其作为参数而不作为输入文件传递给下一个awk命令?

This is nothing very specific for awk. 这对于awk而言并不是很具体。

Either a program can read from stdin, then you can pass the input with a pipe: 程序可以从stdin读取,然后可以使用管道传递输入:

prg1 | prg2 

or your program expects input as parameter, then you use 或您的程序期望输入作为参数,然后使用

prg2 $(prg1) 

Note that in both cases prg1 is processed before prg2. 请注意,在这两种情况下,prg1都在prg2之前处理。

Some programs allow both possibilities, while a huge amount of data is rarely passed as argument. 一些程序允许两种可能性,而很少有大量数据作为参数传递。

This AWK script replaces your whole pipeline: 该AWK脚本替换了整个管道:

awk -v parameter="$(awk '{a[$1]++} END {for (i in a) {if (a[i] > max) {max = a[i]}}; print max}' inputfile)" '{print parameter}' otherfile

where '{print parameter}' is a standin for your other AWK script and "otherfile" is the input for that script. 其中'{print parameter}'是您的其他AWK脚本的替代,而“ otherfile”是该脚本的输入。

Note: It is extremely likely that the two AWK scripts could be combined into one which would be less of a hack than doing it in a way such as that outlined in your question ( awk feeding awk ). 注意:这两个AWK脚本有可能可以组合成一个脚本,而不是像问题中概述的那样做( awk feed awk )。

You can use the shell's $() command substitution: 您可以使用外壳程序的$()命令替换:

awk -f script -v num=$(cut -f1 input.txt | sort | uniq -c | awk -F ' ' '{print $1}' | sort | tail -1) < input_file

(I added the tail -1 to ensure that at most one line is used.) (我添加了tail -1以确保最多使用一行。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM