今天有个人问了这样一个问题,图片是原题,在这个题的基础上写了一个实现方法
首先日志存到a.txt文本里,如下
Zhangsan|lisi1|0|
Zhangsan|lisi2|10|Zhangsan|lisi3|6|Zhangsan|lisi4|0|Zhangsan|lisi5|0|Lisi|list1|0|Lisi|list2|5|
代码如下:
#!/bin/shawk 'BEGIN{FS="|";};{print $1"`"$2"`"$3}' a.txt|sort -t \` -k 1,1|awk 'BEGIN{FS="`";curline="";name="";renshu=0;no_cishu=0;cishu=0};{ if(curline==""){ name=$1; renshu=1; no_cishu=1; chengong=0; cishu=$3; curline="name:"name"`renshu:"renshu"`no_cishu:"no_cishu"`cishu:"cishu"`chengong:"chengong; }else if(name==$1){ renshu++; if($3==0){ no_cishu++; }else{ chengong++; } #print chengong cishu+=$3; }else{ curline="name:"name"`renshu:"renshu"`no_cishu:"no_cishu"`cishu:"cishu"`chengong:"chengong; print curline; name=$1; cishu=$3; renshu=1; no_cishu=1; chengong=0; } }; END{ if(curline!=""){ curline="name:"name"`renshu:"renshu"`no_cishu:"no_cishu"`cishu:"cishu"`chengong:"chengong; print curline; }}'
结果如下:
name:Lisi`renshu:2`no_cishu:1`cishu:5`chengong:1name:Zhangsan`renshu:5`no_cishu:3`cishu:16`chengong:2