Remove Duplicate Line in Vim?

后端 未结 9 1054
有刺的猬
有刺的猬 2021-01-15 06:19

I\'m trying to use VIM to remove a duplicate line in an XML file I created. (I can\'t recreate the file because the ID numbers will change.)

The file looks something

相关标签:
9条回答
  • 2021-01-15 07:06

    You could select the lines then do a :'<,'>sort u if you don't care about the ordering. It will sort and remove duplicates.

    0 讨论(0)
  • 2021-01-15 07:06

    to the OP, if you have bash 4.0

    #!/bin/bash
    # use associative array
    declare -A DUP
    file="myfile.txt"
    while read -r line
    do
        if [ -z ${DUP[$line]} ];then
            DUP[$line]=1
            echo $line >temp
        fi
    done < "$file"
    mv temp "$file"
    
    0 讨论(0)
  • 2021-01-15 07:10

    with python to remove all repeated lines:

    #!/usr/bin/env python
    
    import sys
    def remove_identical(filein, fileout) : 
      lines = list()
      for line in open(filein, 'r').readlines() : 
        if line not in lines : lines.append(line)
      fout = open(fileout, 'w')
      fout.write(''.join(lines))
      fout.close()
    
    remove_identical(sys.argv[1], sys.argv[2])
    
    0 讨论(0)
提交回复
热议问题