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

后端 未结 18 2151
生来不讨喜
生来不讨喜 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:34

    To remove the line and print the output to standard out:

    sed '/pattern to match/d' ./infile
    

    To directly modify the file – does not work with BSD sed:

    sed -i '/pattern to match/d' ./infile
    

    Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed:

    sed -i '' '/pattern to match/d' ./infile
    

    To directly modify the file (and create a backup) – works with BSD and GNU sed:

    sed -i.bak '/pattern to match/d' ./infile
    

提交回复
热议问题