Removing contiguous duplicate lines in vi without sorting

心已入冬 提交于 2019-12-03 08:17:15

问题


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

回答1:


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

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



回答2:


:%!uniq

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




回答3:


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



回答4:


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

:%!uniq



回答5:


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

this is my answer for you




回答6:


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


来源:https://stackoverflow.com/questions/2738826/removing-contiguous-duplicate-lines-in-vi-without-sorting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!