5

So Lets say I have a list

\Summit
\SummitSign
\sun
\ProgrammingOnABoat
\SunCloud
\SunshineOpenCircled
\sup
\supdsub
\supdsub
\supedot
\suphsol
\suphsub
\Summit
\SummitSign
\sun
\Summit
\boatProgramming
\sup

I do not want to sort the list, I just want to remove identical rows, leaving only the first, like so:

\Summit
\SummitSign
\sun
\ProgrammingOnABoat
\SunCloud
\SunshineOpenCircled
\sup
\supdsub

\supedot
\suphsol
\suphsub




\boatProgramming

or like so:

\Summit
\SummitSign
\sun
\ProgrammingOnABoat
\SunCloud
\SunshineOpenCircled
\sup
\supdsub
\supedot
\suphsol
\suphsub
\boatProgramming

As you can see, it was not sorted, but the latter entries were removed.

  • Possible duplicate of delete repeated line in vim, keep the n-th line in Vim – SibiCoder Jun 18 '16 at 18:38
  • 1
    Actually, this question differ from the one you propose in the sens that this one does not have contiguous duplicates. Seeing the accepted answer on the question, it could not work for this case. (but the other way around would ;) ) – nobe4 Jun 21 '16 at 13:56
5

If you don't care about removing the first occurrence (leaving the last one), you can use the following global command:

g/^\(.*\)\n\_.*\(^\1$\)/d

Decomposing:

g/                     /d   " the global command will delete matched lines
  ^\(.*\)                   " Match anything from start of the line (create a group)
         \n                 " Match the end of the line
           \_.*             " Match any character (with new line) 0 or more time
               \(^\1$\)     " Match the previously matched group

This will find any line that has a duplicate later in the file and delete it.

It will delete duplicate lines but keeping only the last one.


As a workaround for the seek of keeping only the first occurence, you can use

 g/^/move 0

Before and after the substitution, this will reverse the order of the file, thus keeping the first line.

  • 1
    Awesome effort. Who knew regex could be readable? – Akiva Jun 21 '16 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.