1. 程式人生 > >awk 練習題 - 陣列、最值、總計

awk 練習題 - 陣列、最值、總計

將以下文字以inode為標記,對inode相同的counts進行累加,並且統計出同一inode中,beginnumber的最小值和endnumber的最大值。

inode|beginnumber|endnumber|counts|
106|3363120000|3363129999|10000|
106|3368560000|3368579999|20000|
310|3337000000|3337000100|101|
310|3342950000|3342959999|10000|
310|3362120960|3362120961|2|
311|3313460102|3313469999|9898|
311|3313470000|3313499999|30000|
311|3362120962|3362120963|2|

輸出的結果格式為:

310|3337000000|3362120961|10103|  
311|3313460102|3362120963|39900|  
106|3363120000|3368579999|30000|

解答:

awk -F'|' -v OFS='|' '/^[0-9]/{inode[$1]++; if(!bn[$1]){bn[$1]=$2}else if(bn[$1]>$2){bn[$1]=$2}; if(en[$1]<$3)en[$1]=$3;cnt[$1]+=$(NF-1)} END{for(i in inode)print i,bn[i],en[i],cnt[i]}' inputfile
[[email protected]
~]# cat t8 inode|beginnumber|endnumber|counts| 106|3363120000|3363129999|10000| 106|3368560000|3368579999|20000| 310|3337000000|3337000100|101| 310|3342950000|3342959999|10000| 310|3362120960|3362120961|2| 311|3313460102|3313469999|9898| 311|3313470000|3313499999|30000| 311|3362120962|3362120963|2| [[email protected] ~]# awk -F'|' -v OFS='|' '/^[0-9]/{ \ inode[$1]++ \ ;if(!bn[$1]){bn[$1]=$2}else if(bn[$1]>$2){bn[$1]=$2} \ ;if(en[$1]<$3)en[$1]=$3;cnt[$1]+=$(NF-1)} \ END{for(i in inode)print i,bn[i],en[i],cnt[i]}' t8 310|3337000000|3362120961|10103 311|3313460102|3362120963|39900 106|3363120000|3368579999|30000