vim: delete all lines but unique?

I am looking for a way in VIM to delete all lines that are duplicates and only leave unique lines that exist in the file. I would prefer a macro but a command or function would be great.

Say I have a file that has duplicate lines and some uniques:

1Apple
1Apple
2Peach
2Peach
2Peach
3Beer
4Banana
4Banana
4Banana

I want to delete all lines so that all that is left:

3Beer

The one unique line that I REALLY want.

I use sort u all the time to get a unique list by deleting duplicates but I have times I just need the unique line in the file.

Any ideas how to handle this case in VIM?

1

4 Answers

You can do this with the substitute command and a backreference:

s/^\(.*\)\(\n\1\)\+//

Note that this only works if the lines are sorted. This works by matching and removing the first group one or more times on the next line(s).

1

You could use the "uniq" command to do this -

uniq -u filename-to-check.txt
2
  1. Select all the lines you want to filter.

  2. Do :'<,'>!uniq -u<CR>, the '<,'> range is added automatically for you.

If you want to filter the whole buffer, don't select anything and simply do :%!uniq -u<CR>.

You can read about filters in :h filter.

3

You can invoke the uniq tool as a filter directly from Vim:

:!%uniq -u

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like