Removing contiguous duplicate lines in vi without sorting
Asked Answered
M

6

16

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
Marabelle answered 29/4, 2010 at 16:2 Comment(0)
M
28

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

g/^\(.*\)$\n\1$/d
Marabelle answered 29/4, 2010 at 16:4 Comment(0)
R
23
:%!uniq

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

Rabble answered 29/4, 2010 at 16:4 Comment(1)
Good suggestion, I should have mentioned in the question that I'm on Windows (and no Cygwin).Marabelle
C
14

If you want to remove non-contiguous duplicates you could use

:g/^\(.*\)\ze\n\%(.*\n\)*\1$/d

(which will remove all but the last copy of a line)

which would change

Foo
Bar
Foo
Bar
Foo
Baz
Foo
Quux

to

Bar
Baz
Foo
Quux

If you want to remove all but the first copy, try

:g/^/m0
:g/^\(.*\)\ze\n\%(.*\n\)*\1$/d
:g/^/m0

which would change

Foo
Bar
Foo
Bar
Foo
Baz
Foo
Quux

to

Foo
Bar
Baz
Quux
Cellular answered 30/4, 2010 at 16:49 Comment(0)
M
3

If you just want to remove contiguous duplicate lines, just use uniq without sorting anything.

:%!uniq
Mint answered 29/4, 2010 at 16:4 Comment(0)
F
0
:%s/^\(.*\)\(\n\1\)\+$/\1/ge

this is my answer for you

Fai answered 30/4, 2014 at 6:49 Comment(0)
R
-4

I know this is old, but it's worth mentioning the following also works if you don't mind sorting as well (I know the OP wanted to avoid it):

:sort u
Roach answered 3/10, 2016 at 6:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.