Linux - Aggregate data and count lines

April 2017



Issue


I have a file containing:
A;1;C;X;123456  
A;1;C;X;234567  
A;1;C;Y;345678  
A;2;D;X;456789  
B;1;D;X;567890  
B;1;D;X;567891  
B;1;D;X;567892 

And I would like to aggregate the data and count lines to get this:
2;A;1;C;X  
1;A;1;C;Y  
1;A;2;D;X  
3;B;1;D;X 


Occurrences will be based on the first column.
How to achieve the following using AWK.

Solution


Try this:
2;A;1;C;X  
1;A;1;C;Y  
1;A;2;D;X  
3;B;1;D;X  

$ cat brol  
A;1;C;X;123456 
A;1;C;X;234567 
A;1;C;Y;345678 
A;2;D;X;456789 
B;1;D;X;567890 
B;1;D;X;567891 
B;1;D;X;567892 

$ uniq -c -w7 brol | cut -d';' -f 1-4 
      2 A;1;C;X 
      1 A;1;C;Y 
      1 A;2;D;X 
      3 B;1;D;X 

$


Thanks to zipe31 for this tip.

Related


Published by deri58. Latest update on February 28, 2013 at 08:32 AM by deri58.
This document, titled "Linux - Aggregate data and count lines," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).