分别将两个字段相加

时间:2013-06-04 20:47:03

标签: bash awk

输入文件abc.txt有COUNTS和NUM列,我想分别为所有COUNTS和NUM的SUM。一旦COUNT是最后一条记录(一个区块),则NUM是最后一条记录(其他区块)。

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

1 个答案:

答案 0 :(得分:1)

awk中快速而肮脏的方式:

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