How to delete from a text file, all lines that contain a specific string?

后端 未结 18 2148
生来不讨喜
生来不讨喜 2020-11-22 02:06

How would I use sed to delete all lines in a text file that contain a specific string?

18条回答
  •  一生所求
    2020-11-22 02:36

    I have made a small benchmark with a file which contains approximately 345 000 lines. The way with grep seems to be around 15 times faster than the sed method in this case.

    I have tried both with and without the setting LC_ALL=C, it does not seem change the timings significantly. The search string (CDGA_00004.pdbqt.gz.tar) is somewhere in the middle of the file.

    Here are the commands and the timings:

    time sed -i "/CDGA_00004.pdbqt.gz.tar/d" /tmp/input.txt
    
    real    0m0.711s
    user    0m0.179s
    sys     0m0.530s
    
    time perl -ni -e 'print unless /CDGA_00004.pdbqt.gz.tar/' /tmp/input.txt
    
    real    0m0.105s
    user    0m0.088s
    sys     0m0.016s
    
    time (grep -v CDGA_00004.pdbqt.gz.tar /tmp/input.txt > /tmp/input.tmp; mv /tmp/input.tmp /tmp/input.txt )
    
    real    0m0.046s
    user    0m0.014s
    sys     0m0.019s
    

提交回复
热议问题