使用awk命令安排数据

时间:2015-01-29 05:57:55

标签: awk solaris

我在文件名atp.txt中有以下数据:

Date_Time,Trx_ID,Message_ID,Status,Description,Amount,Trx_Type
2015-01-27 00:00:00 238,2057723456,BD9012700000003,200,Transaction Successful,1300,PRETOP
2015-01-27 00:00:00 253,2057724418,BD9012700000002,200,Transaction Successful,1600,PRETOP
2015-01-27 00:00:00 270,2057724430,BD9012700000001,200,Transaction Successful,2500,PRETOP
2015-01-27 00:00:00 430,2057724414,BD9012700000005,483,Customer next refill can be done after 10 minutes of successfull refill with same amount.,20150127000000425,
2015-01-27 00:00:00 523,2057723449,BD9012700000004,200,Transaction Successful,1500,POSTBILL
2015-01-27 00:00:04 858,2057724494,BD9012700000016,200,Transaction Successful,2000,PRETOP
2015-01-27 00:00:04 858,2057724485,BD9012700000015,200,Transaction Successful,1100,POSTBILL

我使用以下命令按照我的要求安排。

awk -F',' '{print $4","$7","$5}' atp.txt|sort -r|uniq -c

输出如下:

1 Status,Trx_Type,Description
   8 515,,
  42 500,,Internal System Error.
   1 485,,Request recieved within a block time for Consecutive requests.
   4 484,,
2945 483,,Customer next refill can be done after 10 minutes of successfull refill with same amount.
 573 454,,Recharge not allowed for this subscriber.
  95 404,,Subscriber not found.
238527 200,PRETOP,Transaction Successful
 770 200,POSTBILL,Transaction Successful
  51 134,,Invalid PostPaid Bill payment amount.

但我想在稍后显示计数值。 如下所示:

Status,Trx_Type,Description,1
200,POSTBILL,Transaction Successful,770
200,PRETOP,Transaction Successful,238527
515,,8,
454,,Recharge not allowed for this subscriber.,573

请帮我修改命令。

2 个答案:

答案 0 :(得分:0)

只需将uniq -c的输出传输到另一个快速的Awk或sed脚本,按照您喜欢的方式按下输出。

... | uniq -c | sed '/^ *\([1-9][0-9]*\) *\(.*\)/\2,\1/'

根据您拥有的数据量,您也可以在Awk脚本中生成此数据。但是,这将在Awk中消耗更多内存。

awk -F',' '{++i[$4","$7","$5]}
    END {for (e in i) print e "," i[e] }' atp.txt | sort -r

答案 1 :(得分:0)

您可以一次处理一行结果:

... | uniq -c | while read count rest; do
   echo "${rest},${count}"
done