user3299633
user3299633

Reputation: 3380

Add line numbers for duplicate lines in a file

My text file would read as:

111
111
222
222
222
333
333

My resulting file would look like:

1,111
2,111
1,222
2,222
3,222
1,333
2,333

Or the resulting file could alternatively look like the following:

1
2
1
2
3
1
2

I've specified a comma as a delimiter here but it doesn't matter what the delimeter is --- I can modify that at a future date.In reality, I don't even need the original text file contents, just the line numbers, because I can just paste the line numbers against the original text file.

I am just not sure how I can go through numbering the lines based on repeated entries.

All items in list are duplicated at least once. There are no single occurrences of a line in the file.

Upvotes: 1

Views: 439

Answers (4)

agc
agc

Reputation: 8406

  1. Software tools method, given textfile as input:

    uniq -c textfile | cut -d' ' -f7 | xargs -L 1 seq 1
    
  2. Shell loop-based variant of the above:

    uniq -c textfile | while read a b ; do seq 1 $a ; done
    

Output (of either method):

1
2
1
2
3
1
2

Upvotes: 0

choroba
choroba

Reputation: 241848

Perl solution:

perl -lne 'print ++$c{$_}' file
  • -n reads the input line by line
  • -l handles newlines
  • ++$c{$_} increments the value assigned to the contents of the current line $_ in the hash table %c.

Upvotes: 0

Barmar
Barmar

Reputation: 780929

Use a variable to save the previous line, and compare it to the current line. If they're the same, increment the counter, otherwise set it back to 1.

awk '{if ($0 == prev) counter++; else counter = 1; prev=$0; print counter}'

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 203463

$ awk -v OFS=',' '{print ++cnt[$0], $0}' file
1,111
2,111
1,222
2,222
3,222
1,333
2,333

Upvotes: 4

Related Questions