Sum of column matching pattern/string

Hi All,

I have the following output file created as a result of one script called pattern_daily.log

$ cat pattern_daily.log
Approved|2|Wed, Oct 24, 2012 11:21:09 AM
Declined|1|Wed, Oct 24, 2012 11:21:15 AM
Approved|2|Wed, Oct 24, 2012 11:24:08 AM
Declined|1|Wed, Oct 24, 2012 11:24:18 AM
Approved|2|Wed, Oct 24, 2012 11:25:41 AM
Declined|1|Wed, Oct 24, 2012 11:25:51 AM

Now, I want to sum the second column for each of the Approved and Declined patternfrom this pattern_daily.log file, I used below AWK command

awk -F "|" '{array[$1]+=$2} END { for (i in array) {print i"|" array}}' pattern_daily.log

Got below output:

Approved|6
Declined|3

But the desired output is:

 Approved|6|DATE
 Declined|3|DATE
 

Can someone help please? Perl or shell commands will also do, as I'm finding it difficult with AWK.

How you want to display dates???comma separated fields or just as a string "DATE". If string "DATE", then here you have it in red...but i guess its not

Hi msabhi .. I want to populate the DATE field as a time/date(system date) when this file is getting created. It can be normal or some formatted date.

Hope this helps...

awk -F "|" '{array[$1]+=$2} END { for (i in array) {print i"|" array"|"d}}' "d=$(date)" input_file
1 Like

Thanks Msabhi... given command is working.:slight_smile: