VIM删除重复行

blogdaren 2014-10-27 抢沙发 1540人次
The following command will sort all lines and remove duplicates (keeping unique lines):
:sort u tips:  here "u" maybe short of the word "unique"  I guess:) 
If you need more control, here are some alternatives.
There are two versions, the first leaves only the last line, the second leaves only the first line.
g/^\(.*\)$\n\1$/d
g/\%(^\1$\n\)\@<=\(.*\)$/d 
Breakdown of the second version:

g//d <-- Delete the lines matching the regexp
\@<= <-- If the bit following matches, make sure the bit preceding this symbol directly precedes the match
\(.*\)$ <-- Match the line into subst register 1
\%( ) <--- Group without placing in a subst register.
^\1$\n <--- Match subst register 1 followed by end of line and the new line between the 2 lines In this simple 

format (matching the whole line), it's not going to make much difference, but it will start to matter if you want to do stuff like match the first word only. 

This does a uniq on the first word in the line, and deletes all but the first line:

g/\%(^\1\>.*$\n\)\@<=\(\k\+\).*$/d

版权声明:除非注明,本文由( blogdaren )原创,转载请保留文章出处。

本文链接:VIM删除重复行

发表评论:

您的昵称:
电子邮件:
个人主页:

Free Web Hosting