Score:1

How to count the number of consecutive identical lines

in flag

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

hr flag
This sounds like exactly what `uniq -c` was invented for ...
Joe Jobs avatar
in flag
Oh nice I did not know that. Thanks!
bac0n avatar
cn flag
`uniq -c file | sed -rn 's/ *([0-9]+) (.*)/\2 - \1/p'`
Score:1
cn flag

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
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.