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

后端 未结 3 805
梦如初夏
梦如初夏 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

    You were very close. When you use a nonstandard character for a pattern delimiter, such as |pattern|, the first use of that character must be escaped:

    $ sed '\|^/dev/xvdb|d' /etc/fstab
    /dev/xvda1 / ext4 defaults 1 1
    /dev/md0    /mnt/ibsraid    xfs defaults,noatime    0   2
    /mnt/ibsraid/varlog /var/log    none    bind    0   0
    

    Similarly, one can use:

    sed '\?^/dev/xvdb?d' /etc/fstab
    

    Lastly, it is possible to use slashes inside of /pattern/ if they are escaped in the way that you showed in your answer.

提交回复
热议问题