I have a file with one column with names that repeat a number of times each. I want to condense each repeat into one, while keeping any other repeats of the same name that are not adjacent to other repeats of the same name.
E.g. I want to turn the left side to the right side:
Golgb1 Golgb1
Golgb1 Akna
Golgb1 Spata20
Golgb1 Golgb1
Golgb1 Akna
Akna
Akna
Akna
Spata20
Spata20
Spata20
Golgb1
Golgb1
Golgb1
Akna
Akna
Akna
This is what I've been using: perl -ne 'print if ++$k{$_}==1' file.txt > file2.txt
However, this method only keeps one representative from the left (i.e. Golb1 and Akna are not repeated).
Is there a way to keep unique names for each block, while keeping names that repeat in multiple, non-adjacent blocks?
Best Answer
uniq
will do this for you: