Sum of column matching pattern/string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum of column matching pattern/string
# 1  
Old 10-24-2012
Data 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

Code:
$ 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

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

Got below output:

Code:
Approved|6
Declined|3

But the desired output is:

Code:
 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.
# 2  
Old 10-24-2012
Quote:
Originally Posted by Gem_In_I
Hi All,

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

Code:
$ 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

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

Got below output:

Code:
Approved|6
Declined|3

But the desired output is:

Code:
 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
# 3  
Old 10-24-2012
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.
# 4  
Old 10-24-2012
Hope this helps...
Code:
awk -F "|" '{array[$1]+=$2} END { for (i in array) {print i"|" array[i]"|"d}}' "d=$(date)" input_file

This User Gave Thanks to msabhi For This Post:
# 5  
Old 10-25-2012
Thanks Msabhi... given command is working.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum column values matching other field

this is part of a KT i am going thru. i am writing a script in bash shell, linux where i have 2 columns where 1st signifies the nth hour like 00, 01, 02...23 and 2nd the file size. sample data attached. Desired output is 3 columns which will give the nth hour, number of entries in nth hour and... (3 Replies)
Discussion started by: alpha_1
3 Replies

2. UNIX for Dummies Questions & Answers

String pattern matching and position

I am not an expert with linux, but following various posts on this forum, I have been trying to write a script to match pattern of charters occurring together in a file. My file has approximately 200 million characters (upper and lower case), with about 50 characters per line. I have merged all... (5 Replies)
Discussion started by: biowizz
5 Replies

3. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

4. Shell Programming and Scripting

Breaking the sum of a column when specific pattern is found

Hi Am trying to sum up a file # cat /Out maths 2 0.0 english 2091 0.2 history -1 0.0 physics 18 0.0 -------------------------------------- maths ... (9 Replies)
Discussion started by: Priya Amaresh
9 Replies

5. UNIX for Dummies Questions & Answers

Extracting sub-string matching the pattern.

Hi, I have a string looks like the following: USERS 32767.9844 UNDOTBS1 32767.9844 SYSAUX 32767.9844 SYSTEM 32767.9844 EMS 8192 EMS 8192 EMS_INDEXES 4096 EMS_INDEXES 4096 8 rows selected. How do I extract a sub-string to get the expected output as following: EMS 8192 EMS_INDEXES 4096 ... (3 Replies)
Discussion started by: NetBear
3 Replies

6. Shell Programming and Scripting

Fetching string after matching pattern from last

I have a file a file having entries are like @ram@sham@sita @krishan@kumar @deep@kumar@hello@sham in this file all line are having different no of pattern-@. need to fetch the substring after the last pattern. like sita kumar sham thanks in advance (3 Replies)
Discussion started by: saluja.deepak
3 Replies

7. Shell Programming and Scripting

Get matching string pattern from a file

Hi, file -> temp.txt cat temp.txt /home/pradeep/123/a_asp.html /home/pradeep/123/a_asp1.html /home/pradeep/435/a_asp2.html /home/pradeep/arun/abc/a_dfr.html /home/pradeep/arun/123/a_kir.html /home/pradeep/123/arun/a_dir.html .... .... .. i need to get a_*.html(bolded strings... (4 Replies)
Discussion started by: pradebban
4 Replies

8. Shell Programming and Scripting

awk pattern matching problem -not recognizing a column

Hi all, I am new to awk. I want to print the line numbers if the column has a particular value. For example I have: cat FILE1 COL1 COL2 X114 0 X116 0 X117 0 X120 0 X121 0 X125 0 X126 0 X127 0 X131 1 X132 0 X135 0 X136 0 (3 Replies)
Discussion started by: newpro
3 Replies

9. Shell Programming and Scripting

Extracting a string matching a pattern from a line

Hi All, I am pretty new to pattern matching and extraction using shell scripting. Could anyone please help me in extracting the word matching a pattern from a line in bash. Input Sample (can vary between any of the 3 samples below): 1) Adaptec SCSI RAID 5445 2) Adaptec SCSI 5445S RAID 3)... (8 Replies)
Discussion started by: jharish
8 Replies

10. Shell Programming and Scripting

bash script, pattern matching + sum

Hi guys, i have been struggling to achieve the following and therefor looking for some help. I am trying to write something that will summerize the following output from my switches for daily totals. Basicly if $1 $2 $3 doesnt change, we can keep adding up $4. Probably would use a awk print end... (3 Replies)
Discussion started by: Wratholix
3 Replies
Login or Register to Ask a Question