Search code examples
linuxbashshellgrepwc

How to add number of identical line next to the line itself?


I have file file.txt which look like this

a
b
b
c
c
c

I want to know the command to which get file.txt as input and produces the output

a 1
b 2
c 3

Solution

  • I think uniq is the command you are looking for. The output of uniq -c is a little different from your format, but this can be fixed easily.

    $ uniq -c file.txt
          1 a
          2 b
          3 c