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

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

    Just in case someone wants to do it for exact matches of strings, you can use the -w flag in grep - w for whole. That is, for example if you want to delete the lines that have number 11, but keep the lines with number 111:

    -bash-4.1$ head file
    1
    11
    111
    
    -bash-4.1$ grep -v "11" file
    1
    
    -bash-4.1$ grep -w -v "11" file
    1
    111
    

    It also works with the -f flag if you want to exclude several exact patterns at once. If "blacklist" is a file with several patterns on each line that you want to delete from "file":

    grep -w -v -f blacklist file
    

提交回复
热议问题