Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing duplicate rows in vi?

Tags:

vim

duplicates

I have a text file that contains a long list of entries (one on each line). Some of these are duplicates, and I would like to know if it is possible (and if so, how) to remove any duplicates. I am interested in doing this from within vi/vim, if possible.

like image 990
Sydius Avatar asked Dec 08 '08 22:12

Sydius


People also ask

How do you remove duplicates in Unix?

The uniq command in UNIX is a command line utility for reporting or filtering repeated lines in a file. It can remove duplicates, show a count of occurrences, show only repeated lines, ignore certain characters and compare on specific fields.

How do I remove duplicate lines in files?

Remove duplicate lines with uniq If you don't need to preserve the order of the lines in the file, using the sort and uniq commands will do what you need in a very straightforward way. The sort command sorts the lines in alphanumeric order. The uniq command ensures that sequential identical lines are reduced to one.

How do you duplicate a line in vi?

Directions: Press the ESC key to be sure you are in vi Command mode. Place the cursor on the line you wish to copy. Type yy to copy the line.


2 Answers

If you're OK with sorting your file, you can use:

:sort u 
like image 163
Brian Carper Avatar answered Oct 12 '22 22:10

Brian Carper


Try this:

:%s/^\(.*\)\(\n\1\)\+$/\1/ 

It searches for any line immediately followed by one or more copies of itself, and replaces it with a single copy.

Make a copy of your file though before you try it. It's untested.

like image 24
Sean Avatar answered Oct 12 '22 23:10

Sean