Group by using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Group by using awk
# 1  
Old 10-27-2011
Question Group by using awk

Hi All,

I have a file in the following format...
*****************************************************
Code:
[DEBUG    ] [0] 11/10/27 12:09 : Input Record [2201|11|2215165|B00|0|A4|||0|B00|0|||0|0.000000||2|192.00||nm|0|-1||||||||chander|8015|1211|||||S03_AU480|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|6971982|R90|0|iU|||0|R90|0|||0|0.000000||2|3.36||nm|0|-1||||||||ROH_CEN3|8015|1209|||||ROH_CEN3|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|2215460|2Z1|0|A7|||0|2Z1|0|||0|0.000000||2|115.00||nm|0|-1||||||||chander|8015|1211|||||S03_AU480|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|2215178|2Z1|0|A4|||0|2Z1|0|||0|0.000000||2|130.00||nm|0|-1||||||||chander|8015|1211|||||S03_AU480|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|2216029|2Z1|0|A4|||0|2Z1|0|||0|0.000000||2|143.00||nm|0|-1||||||||chander|8015|1211|||||S03_AU480|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|2215164|B00|0|A4|||0|B00|0|||0|0.000000||2|89.00||nm|0|-1||||||||chander|8015|1211|||||S03_AU480|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|2217457|2Z1|0|A7|||0|2Z1|0|||0|0.000000||2|114.00||nm|0|-1||||||||chander|8015|1211|||||S03_AU480|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|2216024|2Z1|0|A7|||0|2Z1|0|||0|0.000000||2|148.00||nm|0|-1||||||||chander|8015|1211|||||S03_AU480|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|6608598|65Z|0|iS|||0|65Z|0|||0|0.000000||2|1.28||nm|0|-1||||||||ROH_CEN5|8015|1209|||||ROH_CEN5|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|6608598|65Z|0|iT|||0|65Z|0|||0|0.000000||2|11.80||nm|0|-1||||||||ROH_CEN5|8015|1209|||||ROH_CEN5|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|6608598|65Z|0|iU|||0|65Z|0|||0|0.000000||2|1.29||nm|0|-1||||||||ROH_CEN5|8015|1209|||||ROH_CEN5|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|6949785|65Z|0|iU|||0|65Z|0|||0|0.000000||2|0.34||nm|0|-1||||||||urvi|8015|1211|||||ROH_CEN4|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|6622043|65Z|0|iS|||0|65Z|0|||0|0.000000||2|0.25||nm|0|-1||||||||urvi|8015|1211|||||ROH_CEN4|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|6622043|65Z|0|iT|||0|65Z|0|||0|0.000000||2|1.00||nm|0|-1||||||||urvi|8015|1211|||||ROH_CEN4|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|6622043|65Z|0|iU|||0|65Z|0|||0|0.000000||2|202.56||nm|0|-1||||||||urvi|8015|1211|||||ROH_CEN4|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|6622043|65Z|0|iS|||0|65Z|0|||0|0.000000||2|0.22||nm|0|-1||||||||ROH_CEN4|8015|1021|C|||Y|ROH_CEN4|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|6608545|R90|0|iU|||0|R90|0|||0|0.000000||2|3.27||nm|0|-1||||||||ROH_CEN3|8015|1209|||||ROH_CEN3|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|6598660|65Z|0|iU|||0|65Z|0|||0|0.000000||2|6.79||nm|0|-1||||||||urvi|8015|1211|||||ROH_CEN4|
[DEBUG    ] [0] 11/10/27 12:10 : Input Record [2201|11|7508006|65Z|0|iU|||0|65Z|0|||0|0.000000||2|8.04||nm|0|-1||||||||urvi|8015|1211|||||ROH_CEN4|

******************************************************

I am looking for an awk to see how many lines are there grouping by the last field i.e. ROH_CEN3 or S03_AU480 etc. I expect to see it like...
Code:
ROH_CEN3 | 50
S03_AU480 | 15
ROH_CEN4 | 43

Can any one of you please give me a hand ?

Regards


Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 10-27-2011 at 04:12 AM.. Reason: Please use code tags, thank you
# 2  
Old 10-27-2011
For the posted input file ..
Code:
$ nawk -F\| '{print $(NF-1)|"sort|uniq -c"}' infile
   2 ROH_CEN3
   7 ROH_CEN4
   3 ROH_CEN5
   7 S03_AU480

This User Gave Thanks to jayan_jay For This Post:
# 3  
Old 10-27-2011
Or maybe with:
Code:
awk -F"|" '{a[$(NF-1)]++}END{for(i in a) print i FS a[i]}' file

This User Gave Thanks to Franklin52 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Group and search using awk

file: Data has to be grouped on 1st field; And in each group, print 2nd field IF 1st field doesn't match with any of the values in 2nd field.. o/p to be: I ended up writing as below, any help ?? awk -F, '!($1 in a){a++;}END{for (i in a)print i}' file (3 Replies)
Discussion started by: JSKOBS
3 Replies

2. Shell Programming and Scripting

Awk: get upper and lower bound per group

Hi all, I've data as: 22 51018157 51018157 exonic CHKB nonsynonymous SNV 22 51018204 51018204 exonic CHKB nonsynonymous SNV 22 51018428 51018428 exonic CHKB nonsynonymous SNV 22 51018814 51018814 ... (4 Replies)
Discussion started by: genome
4 Replies

3. Shell Programming and Scripting

How to add using awk command for a group?

hi all below is the input file contains a code followed by amount input file 345,5.86 346,20.58 399,10.00 400,12.00 i need the output in the following way outputfile totalsumof345and346,26.44 totalsumof399and400,22.00 (10 Replies)
Discussion started by: hemanthsaikumar
10 Replies

4. Shell Programming and Scripting

awk Group By and count string occurrences

Hi Gurus, I'm scratching my head over and over and couldn't find the the right way to compose this AWK properly - PLEASE HELP :confused: Input: c,d,e,CLICK a,b,c,CLICK a,b,c,CONV c,d,e,CLICK a,b,c,CLICK a,b,c,CLICK a,b,c,CONV b,c,d,CLICK c,d,e,CLICK c,d,e,CLICK b,c,d,CONV... (6 Replies)
Discussion started by: Royi
6 Replies

5. Shell Programming and Scripting

awk - how do i get the last row of a group

How do i print the last record of a group in a file ? For example, I have a file like this : cat txt cucm1,location1,1,2,3 cucm2,location1,3,4,5 cucm1,location1,10,20,30 cucm2,location2,30,40,50 I am expecting a command that would print the last record of the group. For example, ... (2 Replies)
Discussion started by: Lakshmikumari
2 Replies

6. Shell Programming and Scripting

Awk-using group function

Hi, I have file with below format and sample data - File is pipe delimited Col1|col2|Account|Bal1|Bal2 1|2|1|10|5 1|2|2|10|2 1|3|3|10|3 I want output as SUM|1|2|2|20|7 SUM|1|3|1|10|3 Can anyone give me awk command (4 Replies)
Discussion started by: sanranad
4 Replies

7. Infrastructure Monitoring

Processing records as group - awk

I have a file has following records policy glb id 1233 name Permit ping from "One" to "Second" "Address1" "Any" "ICMP-ANY" permit policy id 999251 service "snmp-udp" exit policy glb id 1234 name Permit telnet from "One" to "Second" "Address2" "Any" "TCP-ANY" permit policy id 1234... (3 Replies)
Discussion started by: baskar
3 Replies

8. Shell Programming and Scripting

awk group by

Hi I have a file in this format: ... 04/May/2009 16 04/May/2009 1 05/May/2009 3 05/May/2009 5 06/May/2009 1 06/May/2009 3 ... I need to sum for every day, What is the best way? Thanks all (2 Replies)
Discussion started by: mutti
2 Replies

9. Shell Programming and Scripting

Awk-Group count of field

Hi, Suppose if i am having a file with following records as given below. 5555 6756 5555 4555 4555 6767 how can i get the count of each record using AWK. Eg:5555 count should be 2 4555 count should be 2 6767 count should be 1 ... (5 Replies)
Discussion started by: tinivt
5 Replies

10. Shell Programming and Scripting

awk help required to group output and print a part of group line and original line

Hi, Need awk help to group and print lines to format the output as shown below INPUT FORMAT set echo on set heading on set spool on /* SCHEMA1 */ CREATE TABLE T1; /* SCHEMA1 */ CREATE TABLE T2; /* SCHEMA1 */ CREATE TABLE T3; /* SCHEMA1 */ CREATE TABLE T4; /* SCHEMA1 */ CREATE TABLE T5;... (5 Replies)
Discussion started by: rajan_san
5 Replies
Login or Register to Ask a Question