用sed修改配置文件

通过模式定位到指定的配置项,对相应行进行整行替换实现对配置文件的修改,不过这样行后注释也会被替换掉,但目前sed不支持零宽断言的正则表达式语法,无法对特定位置进行定位修改,所以在修改配置文件的时候用c进行整行替换比s作模式替换来得更实际.

文件内容如下

# Options to syslogd
# syslogd options are deprecated since rsyslog v3
# if you want to use them, switch to compatibility mode 2 by "-c 2"
SYSLOGD_OPTIONS = "-c 4"   #commet

 

SYSLOGD_OPTIONS的值改为"-r -m 0"可这样写命令

 

opt='SYSLOGD_OPTIONS'

sed  "/^\s*$opt[[:space:]]*=/c $opt=\"-r -m 0\"" rsyslog

 

输出的内容为

 

# Options to syslogd
# syslogd options are deprecated since rsyslog v3
# if you want to use them, switch to compatibility mode 2 by "-c 2"
SYSLOGD_OPTIONS="-r -m 0"

你可能感兴趣的:(C++,c,正则表达式,C#)