一、查找指定字符串
a、比如我要查找并显示下面文件显示-s之后的数字
-
root@Phoenix /service/stage2/syslog#cat run
-
#!/bin/sh
-
-
exec /sbin/syslogd -n -b 0 -s 4
b、sed命令如下
-
root@Phoenix /service/stage2/syslog#sed -n '/-s.*$/p' run
-
exec /sbin/syslogd -n -b 0 -s 4
-
root@Phoenix /service/stage2/syslog#sed -n '/-s.*$/p' run |sed -e 's/.* //g'
-
4
c、例如要截取上面-b后面的那个数字0
-
root@Phoenix /service/stage2/syslog#sed -n '/-b.*$/p' run|sed -e 's/.*-b //g' -e 's/ -s.*$//g'
-
0
d、例如要修改-b后面的那个数字0为4
-
root@Phoenix /root#cat run
-
#!/bin/sh
-
-
exec /sbin/syslogd -n -b 0 -s
-
root@Phoenix /root#sed -n '/-b/p' run|sed -i '/\(-s.*$\)/s/\(.*-b \)[0-9]/\14\2/
-
g' run
-
root@Phoenix /root#cat run
-
#!/bin/sh
-
-
exec /sbin/syslogd -n -b 4 -s
二、查找指定字符串并修改文件,比如查找-s后面的数字4改成6.
-
root@Phoenix /service/stage2/syslog#sed -n '/-s.*$/p' run | sed -i 's/-s [0-9]*$/-s 6/' run
-
root@Phoenix /service/stage2/syslog#cat run
-
#!/bin/sh
-
-
exec /sbin/syslogd -n -b 0 -s 6
-
root@Phoenix /service/stage2/syslog#
三、实例
sys.conf文件
-
root@Phoenix /root#cat sys.conf
-
eth :
-
{
-
ipaddr = "192.168.5.233";
-
netmask = "255.255.255.0";
-
gateway = "192.168.1.1";
-
mac = "00:01:02:03:04:05";
-
dhcp = 1;
-
};
-
mag :
-
{
-
is_strict = 0;
-
};
-
ppp :
-
{
-
PppAsynmap = 1;
-
PppEscape = 0;
-
PppIpcp = 0;
-
};
-
kdb :
-
{
-
keysensitive = 0;
-
beepvolumn = 1;
-
};
-
root@Phoenix /root#
例一、删除sys.conf配置文件中eth这个节点的配置信息,并修改配置文件。
-
root@Phoenix /root#sed -ie '/^eth/,/};/d' sys.conf
-
root@Phoenix /root#cat sys.conf
-
mag :
-
{
-
is_strict = 0;
-
};
-
ppp :
-
{
-
PppAsynmap = 1;
-
PppEscape = 0;
-
PppIpcp = 0;
-
};
-
kdb :
-
{
-
keysensitive = 0;
-
beepvolumn = 1;
-
};
-
root@Phoenix /root#
例二、查询kdb节点,beepvolumn这个字段的配置值。
-
root@Phoenix /root#sed -n '/^kdb/,/};/p' sys.conf|sed -ne '/beepvolumn =/p'|sed -e 's/.*beepvolumn = //g' -e 's/;$//g'
-
1
-
root@Phoenix /root#
例三、修改kbd节点,beepvolumn这个字段值为10,并修改文件
方法一、
-
root@Phoenix /root#sed -ne '/kdb/,/};/p' sys.conf|sed -n '/.*beepvolumn = /p'|sed -i 's/beepvolumn = .*$/beepvolumn = 10;/' sys.conf
-
root@Phoenix /root#cat sys.conf
-
mag :
-
{
-
is_strict = 0;
-
};
-
ppp :
-
{
-
PppAsynmap = 1;
-
PppEscape = 0;
-
PppIpcp = 0;
-
};
-
kdb :
-
{
-
keysensitive = 0;
-
beepvolumn = 10;
-
};
方法二、
-
root@Phoenix /root#sed -i '/\(;$\)/s/\([ \t]beepvolumn = \)[0-9]\{1,\}/\110\2/g' sys.conf
-
root@Phoenix /root#cat sys.conf
-
mag :
-
{
-
is_strict = 6;
-
};
-
ppp :
-
{
-
PppAsynmap = 6;
-
PppEscape = 6;
-
PppIpcp = 911;
-
};
-
kdb :
-
{
-
keysensitive = 6;
-
beepvolumn = 10;
-
};
-
root@Phoenix /root#
总结:sed命令是不是很好用啊。
阅读(3178) | 评论(1) | 转发(0) |