AWK是如何发生UNIX命令作为参数的结果? [英] how awk takes the result of a unix command as a parameter?

查看:141
本文介绍了AWK是如何发生UNIX命令作为参数的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个与分隔场标签的输入文件,第一个字段是整数

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

然后我需要找到这些计数的最大值,在这种情况下,这是6。

Then I need to find the max of these counts, in this case, it's 6.

现在我需要通过6到另一个awk脚本作为parmeter。

Now i need to pass "6" to another awk script as a parmeter.

我知道我可以使用下面的命令来获得计数的列表:

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 

但我如何得到第一次开始计数,并把它传递给下一个awk命令作为参数而不是输入文件?

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 AWK script replaces your whole pipeline:

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

其中,'{打印参数} 是其他AWK脚本和otherfile一个站在是该脚本的输入。

where '{print parameter}' is a standin for your other AWK script and "otherfile" is the input for that script.

注意:非常可能是两个AWK脚本可以被组合成一个这将是比做得少黑客在某种程度上,如你的问题,概述( AWK 喂养 AWK )。

这篇关于AWK是如何发生UNIX命令作为参数的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆