Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to count the number of consecutive identical lines

I have a file that looks like this:

2000
2000
2001
2001
2001
2001
2002
2002

I need a script to show me this:

2000 - 2
2001 - 4
2002 - 2

I prefer using sed or awk

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is precisely what uniq -c does. From man uniq:

DESCRIPTION

Filter adjacent matching lines from INPUT (or standard input), writing to OUTPUT (or standard output).

[ . . . ]

-c, –count
       prefix lines by the number of occurrences

So with your example, we get:

$ uniq -c file
      2 2000
      4 2001
      2 2002

You can also write a little script if you prefer for some reason. For instance, with awk:

$ awk '{ count[$0]++ } END{ for(line in count){ print line,count[line] }}' file 
2000 2
2001 4
2002 2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading