Reza
Reza

Reputation: 203

Number of occurrences of unique rows in a file

Assume a file has the following rows:

aa bbb cccc dddd
eee ffff ggggg
aa bbb cccc dddd
i jj kkk llll
aa bbb cccc dddd
eee ffff ggggg

Is there any bash command to determine how many times unique rows in the file have occurred? The output should be:

aa bbb cccc dddd 3 time
eee ffff ggggg 2 times
i jj kkk llll 1 time

Upvotes: 1

Views: 124

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 992947

You can combine a couple of commands:

sort <inputfile | uniq -c

The man page for uniq starts with:

NAME
       uniq - report or omit repeated lines

SYNOPSIS
       uniq [OPTION]... [INPUT [OUTPUT]]

DESCRIPTION
       Discard all but one of successive identical lines from INPUT
       (or standard input), writing to OUTPUT (or standard output).

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

So the output for your given input file is:

  3 aa bbb cccc dddd
  2 eee ffff ggggg
  1 i jj kkk llll

Upvotes: 2

Related Questions