Removing duplicate rows in vi?

后端 未结 14 2031
无人及你
无人及你 2021-01-29 23:10

I have a text file that contains a long list of entries (one on each line). Some of these are duplicates, and I would like to know if it is possible (and if so, how) to remove

相关标签:
14条回答
  • 2021-01-29 23:42
    :%s/^\(.*\)\(\n\1\)\+$/\1/gec
    

    or

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

    this is my answer for you ,it can remove multiple duplicate lines and only keep one not remove !

    0 讨论(0)
  • 2021-01-29 23:44

    Try this:

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

    It searches for any line immediately followed by one or more copies of itself, and replaces it with a single copy.

    Make a copy of your file though before you try it. It's untested.

    0 讨论(0)
  • 2021-01-29 23:45

    I would combine two of the answers above:

    go to head of file
    sort the whole file
    remove duplicate entries with uniq
    
    1G
    !Gsort
    1G
    !Guniq
    

    If you were interested in seeing how many duplicate lines were removed, use control-G before and after to check on the number of lines present in your buffer.

    0 讨论(0)
  • 2021-01-29 23:46

    Regarding how Uniq can be implemented in VimL, search for Uniq in a plugin I'm maintaining. You'll see various ways to implement it that were given on Vim mailing-list.

    Otherwise, :sort u is indeed the way to go.

    0 讨论(0)
  • 2021-01-29 23:47

    Select the lines in visual-line mode (Shift+v), then :!uniq. That'll only catch duplicates which come one after another.

    0 讨论(0)
  • 2021-01-29 23:48

    If you don't want to sort/uniq the entire file, you can select the lines you want to make uniq in visual mode and then simply: :sort u.

    0 讨论(0)
提交回复
热议问题