使用ubuntu中的sed将具有特殊字符的块替换为另一个块

时间:2016-01-19 07:47:43

标签: regex bash sed ubuntu-14.04

我正在尝试自动替换tripwire配置文件中的某些行:

我在文件中有以下部分:

#
# Critical devices
#
(
  rulename = "Devices & Kernel information",
  severity = $(SIG_HI),
)
{
    /dev        -> $(Device) ;
    /proc       -> $(Device) ;
}

我想用它替换它(更改从/ dev / pts行开始)

#
# Critical devices
#
(
  rulename = "Devices & Kernel information",
  severity = $(SIG_HI),
)
{
    /dev        -> $(Device) ;
    /dev/pts        -> $(Device) ;
        #/proc                  -> $(Device) ;
        /proc/devices           -> $(Device) ;
        /proc/net               -> $(Device) ;
        /proc/tty               -> $(Device) ;
        /proc/sys               -> $(Device) ;
        /proc/cpuinfo           -> $(Device) ;
        /proc/modules           -> $(Device) ;
        /proc/mounts            -> $(Device) ;
        /proc/dma               -> $(Device) ;
        /proc/filesystems       -> $(Device) ;
        /proc/interrupts        -> $(Device) ;
        /proc/ioports           -> $(Device) ;
        /proc/scsi              -> $(Device) ;
        /proc/kcore             -> $(Device) ;
        /proc/self              -> $(Device) ;
        /proc/kmsg              -> $(Device) ;
        /proc/stat              -> $(Device) ;
        /proc/loadavg           -> $(Device) ;
        /proc/uptime            -> $(Device) ;
        /proc/locks             -> $(Device) ;
        /proc/meminfo           -> $(Device) ;
        /proc/misc              -> $(Device) ;
}

我正在尝试使用sed编写命令,但我没有成功。

如果有人可以建议我做什么,我将不胜感激。 谢谢

1 个答案:

答案 0 :(得分:1)

您可以将此sed与~之类的备用正则表达式使用者一起使用,因为您的搜索和替换模式中存在/

使用以下数据创建文件调用new.txt

    /dev/pts        -> $(Device) ;
    #/proc                  -> $(Device) ;
    /proc/devices           -> $(Device) ;
    /proc/net               -> $(Device) ;
    /proc/tty               -> $(Device) ;
    /proc/sys               -> $(Device) ;
    /proc/cpuinfo           -> $(Device) ;
    /proc/modules           -> $(Device) ;
    /proc/mounts            -> $(Device) ;
    /proc/dma               -> $(Device) ;
    /proc/filesystems       -> $(Device) ;
    /proc/interrupts        -> $(Device) ;
    /proc/ioports           -> $(Device) ;
    /proc/scsi              -> $(Device) ;
    /proc/kcore             -> $(Device) ;
    /proc/self              -> $(Device) ;
    /proc/kmsg              -> $(Device) ;
    /proc/stat              -> $(Device) ;
    /proc/loadavg           -> $(Device) ;
    /proc/uptime            -> $(Device) ;
    /proc/locks             -> $(Device) ;
    /proc/meminfo           -> $(Device) ;
    /proc/misc              -> $(Device) ;

然后运行此命令:

sed -e '/\/proc/{s/.*//; r new.txt' -e '}' file

<强>输出:

#
# Critical devices
#
(
  rulename = "Devices & Kernel information",
  severity = $(SIG_HI),
)
{
    /dev        -> $(Device) ;

    /dev/pts        -> $(Device) ;
        #/proc                  -> $(Device) ;
        /proc/devices           -> $(Device) ;
        /proc/net               -> $(Device) ;
        /proc/tty               -> $(Device) ;
        /proc/sys               -> $(Device) ;
        /proc/cpuinfo           -> $(Device) ;
        /proc/modules           -> $(Device) ;
        /proc/mounts            -> $(Device) ;
        /proc/dma               -> $(Device) ;
        /proc/filesystems       -> $(Device) ;
        /proc/interrupts        -> $(Device) ;
        /proc/ioports           -> $(Device) ;
        /proc/scsi              -> $(Device) ;
        /proc/kcore             -> $(Device) ;
        /proc/self              -> $(Device) ;
        /proc/kmsg              -> $(Device) ;
        /proc/stat              -> $(Device) ;
        /proc/loadavg           -> $(Device) ;
        /proc/uptime            -> $(Device) ;
        /proc/locks             -> $(Device) ;
        /proc/meminfo           -> $(Device) ;
        /proc/misc              -> $(Device) ;
}