meleu meleu - 6 months ago 8
Linux Question

How to put sequential numbers only at end of the repeated lines

I have a file with some repeated lines. I want to put a sequential number only at the end of these repetitions.

Example.

Input:

Jose da Silva
Jose da Silva
Fulano de Tal
Jose da Silva
Sicrano Pereira
Ze Ruela
Sicrano Pereira
Jose da Silva


Output:

Jose da Silva #1
Jose da Silva #2
Fulano de Tal
Jose da Silva #3
Sicrano Pereira #1
Ze Ruela
Sicrano Pereira #2
Jose da Silva #4


I cannot sort these lines. They have to appear on the same order.

Answer

Loop twice:

awk 'FNR==NR {count[$0]++; next} 
     count[$0]>1 {$0=$0 FS "#"++times[$0]}
     1' file file

That is: the first time, count how many times each line occurs. The second time, keep appending an incrementing number to those that appear more than once.

$ awk 'FNR==NR {count[$0]++; next} count[$0]>1 {$0=$0 FS "#"++times[$0]}1' file file
Jose da Silva #1
Jose da Silva #2
Fulano de Tal
Jose da Silva #3
Sicrano Pereira #1
Ze Ruela
Sicrano Pereira #2
Jose da Silva #4