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

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

    I was struggling with this on Mac. Plus, I needed to do it using variable replacement.

    So I used:

    sed -i '' "/$pattern/d" $file

    where $file is the file where deletion is needed and $pattern is the pattern to be matched for deletion.

    I picked the '' from this comment.

    The thing to note here is use of double quotes in "/$pattern/d". Variable won't work when we use single quotes.

提交回复
热议问题