Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sed 替换匹配行后面的文本行_Sed - Fatal编程技术网

Sed 替换匹配行后面的文本行

Sed 替换匹配行后面的文本行,sed,Sed,我正在处理的文件如下所示: # # Bulk mode # #define command { # command_name process-service-perfdata-file # command_line /usr/libexec/pnp4nagios/process_perfdata.pl --bulk /var/log/pnp4nagios/service-perfdata #} #define command { # comman

我正在处理的文件如下所示:

#
# Bulk mode
#
#define command {
#       command_name    process-service-perfdata-file
#       command_line    /usr/libexec/pnp4nagios/process_perfdata.pl --bulk /var/log/pnp4nagios/service-perfdata
#}

#define command {
#       command_name    process-host-perfdata-file
#       command_line    /usr/libexec/pnp4nagios/process_perfdata.pl --bulk /var/log/pnp4nagios/host-perfdata
#}

#
# Bulk with NPCD mode
#
#define command {
#       command_name    process-service-perfdata-file
#       command_line    /bin/mv /var/log/pnp4nagios/service-perfdata /var/spool/pnp4nagios/service-perfdata.$TIMET$
#}

#define command {
#       command_name    process-host-perfdata-file
#       command_line    /bin/mv /var/log/pnp4nagios/host-perfdata /var/spool/pnp4nagios/host-perfdata.$TIMET$
#}
我想取消注释“Bulk with NPCD mode”标题后面的所有行。到目前为止,我得到的是取消注释所有内容,包括标题:

在进行任何替换之前,我如何让它跳过“带NPCD模式的批量”行?我尝试了
n
命令,但移动到下一行似乎会停止对其余行的任何进一步处理:

sed -E '/Bulk with NPCD mode/,$ {n; s/^#(.+)/\1/}' /etc/pnp4nagios/misccommands.cfg

使用范围是正确的方法,但是,
sed
将范围边界包含在范围中,这就是为什么需要显式跳过包含搜索模式的起始行:

sed '/Bulk with NPCD/,${/Bulk with NPCD/!s/^#//}'
顺便说一句,
s/^#/
将替换行开头的哈希。

这可能适用于您(GNU-sed):


将当前模式存储在保留空间中,并将其附加到每一行。如果当前模式为带NPCD的
批量模式
,如果不是模式注释行或空注释,则删除注释字符。

如您所见,我已经熟悉范围,但它是带NPCD的
/Bulk/跳过我不熟悉的行的语法。非常感谢。哦,是的。我本该更仔细地阅读这个问题的。很高兴看到它起作用了!
sed '/Bulk with NPCD/,${/Bulk with NPCD/!s/^#//}'
sed '/^#.*mode$/h;//!G;/\n# Bulk with NPCD mode$/{/^#\n\|^# Bulk with NPCD mode\n/!s/^#//};P;d' file