[轉貼] VIM刪除重復行
下面收錄了2篇相關文章
1) 轉貼自:http://kangzye.blog.163.com/blog/static/36819223201061705729933/
vim 查找相同的兩行,以及刪除重復行
要查找相同的兩行,先將內容排序,然后查找前一行等於后一行者
:sort
/^\(.\+\)$\n\1
如此就找到了,博大精深的VIM – . –
刪除重復行(先排一下序):
:sort
:g/^\(.\+\)$\n\1/d
2) 轉貼自:http://kangzye.blog.163.com/blog/static/36819223201061705729933/
關於Vim的刪除文本中的重復行。
最近開始使用Vim,感覺此乃外人間之神器,怪不得那么多Vimer對其偏愛有加。現在說說文本中重讀行刪除問題。在眾多的編輯器中Vim對正則的支持估計是最強大的了,很多高級的正則表達式在Vim中都是支持的。以下是我的解法:
:%s/\(.*\)\n\1/\1\r/g
:g/^$/d
這里需要兩個命令,后再http://vim.wikia.com/wiki/Uniq_-_Removing_duplicate_lines找到了我的加強版:
:g/^\(.*\)$\n\1$/d
:g/\%(^\1$\n\)\@<=\(.*\)$/d
兩者都是前面的命令和我的相似,它刪除的是重復項的前面一項;后面的命令刪除的重復項的后面的內容。注意兩個命令獨立的,可以分別使用。對於g命令不是很熟悉,它是一個全局命令,以后會有全面介紹。
第二個命令的解釋:
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