awk Count Zero和Non Zero字段值:

时间:2014-02-25 09:37:53

标签: unix awk

请帮助识别,第三个字段值等于零的行项目数和第三个字段值不等于零的行项目数,按第二个字段分组: 需要填充" Count!= 0&计数= 0" as" 0"如果没有计数。

Input file: f1.txt
ppp1,abc,10,qqq
ppp2,abc,5,qqq
ppp3,abc,0,qqq
ppp4,abc,18,qqq
ppp5,abc,0,qqq
mmm1,xyz,0,rrr
mmm2,xyz,55,rrr
nnn1,ijk,12,sss
nnn2,ijk,89,sss
nnn3,ijk,62,sss
bbb1,lmn,0,ttt
bbb2,lmn,0,ttt

Output.txt
abc,count!=0,3
abc,count=0,2
xyz,count!=0,1
xyz,count=0,1
ijk,count!=0,3
ijk,count=0,0
lmn,count!=0,0
lmn,count=0,2

2 个答案:

答案 0 :(得分:2)

我打破了行中的cmd,以便于阅读:

 awk -F, -v OFS="," '{c[$2];
                    if($3!=0)a[$2]++;else b[$2]++}
                    END{for(x in c){
                         print x,"count!=0",a[x]*1;
                         print x,"count=0",b[x]*1}}' input

答案 1 :(得分:0)

cat f1.txt | awk -F“,”'{if($ 3 == 0){print $ 2 count = 0} if($ 3!= 0)
{print $ 2 count!= 0}}'| uniq -c | awk {print $ 2“,”$ 3“,”$ 1}>> output.txt的