2013-06-04 100 views
-2

輸入文件abc.txt包含COUNTS和NUM列,我希望SUM分別適用於所有COUNTS和NUM。一旦COUNT是最後一個記錄(一個塊),那麼NUM是最後一個記錄(另一個塊)。在awk做的分別總結兩個字段

COMPANY : ALL COMPANIES 

REQUEST TYPE DESCRIPTION          COUNTS 

A1    ABC (1 2 3)         107 
A2    O REATE (NOT AUTHORIZED)        3 
A3    NEW OR OLD TC           9 
b1    NOTIFY ACTIVE           2 
b2    CANCEL T            8 
AA    CANCEL SERVICE           0 
AC    ACKNOWLEDGE           0 
AB    ACTIVE            128 



ALL COMPANIES 

REQUEST TYPE DESCRIPTION          COUNTS  NUM 

A1    ABC (1 2 3)         107  01 
A2    O REATE (NOT AUTHORIZED)        3  01 
A3    NEW OR OLD TC           9  03 
b1    NOTIFY ACTIVE           2  04 
b2    CANCEL T            8  05 
AA    CANCEL SERVICE           0  07 
AC    ACKNOWLEDGE           0  08 
AB    ACTIVE            128  09 
+0

之所以這麼多的選票下來,你會發現它在這裏 - http://stackoverflow.com/helpcenter/asking –

回答

1

快速和骯髒的方式:

awk 'NF{$(NF-1)~/[a-zA-Z]+/?count+=$NF:num+=$NF;count+=$(NF-1)}END{print "Count= "count" NUM= "num}' inputFile