-2

输入文件 abc.txt 具有 COUNTS 和 NUM 列,我想分别为所有 COUNTS 和 NUM 求和。一旦 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
4

1 回答 1

1

快速而肮脏的做法awk

awk 'NF{$(NF-1)~/[a-zA-Z]+/?count+=$NF:num+=$NF;count+=$(NF-1)}END{print "Count= "count" NUM= "num}' inputFile
于 2013-06-04T21:09:26.977 回答