Linux - Aggregate data and count lines

December 2016



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 :

This document entitled « Linux - Aggregate data and count lines » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.