Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing contiguous duplicate lines in vi without sorting

This question already addresses how to remove duplicate lines, but enforces that the list is sorted first.

I would like to perform the remove contiguous duplicate lines step (i.e. uniq) without first sorting them.

Example before:

Foo
Foo
Bar
Bar

Example after:

Foo
Bar
like image 841
davetapley Avatar asked Apr 29 '10 16:04

davetapley


People also ask

Can we use uniq without sort?

Indeed, both commands can remove duplicate lines from input, for example, a text file. However, the uniq command requires the file to be sorted, and sort first sorts lines in the file. In this tutorial, we'll explore a method to remove duplicate lines from an input file without sorting.

How do I remove duplicate lines in Linux?

The uniq command is used to remove duplicate lines from a text file in Linux. By default, this command discards all but the first of adjacent repeated lines, so that no output lines are repeated. Optionally, it can instead only print duplicate lines.


2 Answers

:%!uniq

if you're on a unix system, or a system that has the uniq program

like image 38
John Weldon Avatar answered Sep 19 '22 09:09

John Weldon


Just found the solution here. The following regex works correctly:

g/^\(.*\)$\n\1$/d
like image 90
davetapley Avatar answered Sep 21 '22 09:09

davetapley