sed: How to delete lines matching a pattern that contains forward slashes?

后端 未结 3 836
梦如初夏
梦如初夏 2021-02-02 14:30

Suppose a file /etc/fstab contains the following:

/dev/xvda1 / ext4 defaults 1 1
/dev/md0    /mnt/ibsraid    xfs defaults,noatime    0   2
/mnt/ibsr         


        
3条回答
  •  自闭症患者
    2021-02-02 15:02

    Why the obsession with using forward slash as your delimiter? Just use something else, like a comma:

    sed ',^/dev/xvdb,d' /etc/fstab
    

    or a colon:

    sed ':^/dev/xvdb:d' /etc/fstab
    

    Or whatever makes it easiest to read. The delimiter can be any character. The convention is to use a forward slash, but when it becomes awkward, switch it to something else.

    Note, if you want to change the file itself, rather than output the result, you need the "in place" flag -i:

    sed -i ':^/dev/xvdb:d' /etc/fstab
    

提交回复
热议问题