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

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

    You may consider using ex (which is a standard Unix command-based editor):

    ex +g/match/d -cwq file
    

    where:

    • + executes given Ex command (man ex), same as -c which executes wq (write and quit)
    • g/match/d - Ex command to delete lines with given match, see: Power of g

    The above example is a POSIX-compliant method for in-place editing a file as per this post at Unix.SE and POSIX specifications for ex.


    The difference with sed is that:

    sed is a Stream EDitor, not a file editor.BashFAQ

    Unless you enjoy unportable code, I/O overhead and some other bad side effects. So basically some parameters (such as in-place/-i) are non-standard FreeBSD extensions and may not be available on other operating systems.

提交回复
热议问题