Perlnika
Perlnika

Reputation: 5066

How to count number of occurencies of the same line?

I have file that looks like this:

1747834 222
1747834 222
1747834 222
1747834 222
2514112 32636
2514112 32636
2514112 32636

For each line I want to count number of its occurencies and write it to the third column, each line should be divided by new line like this:

1747834 222 4

2514112 32636 3

I managed to write this script:

while read line; do a=`grep "$line" input | wc -l`; echo -e ${line}; echo -e "${a}\n"; done < input_uniq

where input_uniq is output of

 cat input | uniq

but unfortunately I get output like this:

1747834 222
4

2514112 32636
3

If I use just one echo, output is very strange (Basically, $line is overwritten by $a at the beginning) So I am looking for another way to do what I need or for suggestion how to fix my script.

Upvotes: 1

Views: 162

Answers (1)

Chris Seymour
Chris Seymour

Reputation: 85795

The standard way is:

sort file | uniq -c 
      4 1747834 222
      3 2514112 32636

Although the line count is append to the start not the end so here is an awk script to do exactly that:

$ awk '{seen[$0]++}END{for (line in seen) print line, seen[line]}' file
1747834 222 4
2514112 32636 3

Upvotes: 2

Related Questions