+ -
当前位置:首页 → 问答吧 → 怎样用sed命令修改、删除、增加配置文件

怎样用sed命令修改、删除、增加配置文件

时间:2011-09-22

来源:互联网

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=5
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=2
# the port at which the clients will connect
clientPort=2181
maxClientCnxns=1000
dataDir=/hds/zk/zdatadir

dataLogDir=/hds/zk/zlogdir
clientPortAddress=172.17.81.110

#server.1=172.17.81.110:2888:3888
#server.2=172.17.81.111:2888:3888
#server.3=172.17.81.113:2888:3888



上面是我的一个配置文件,现在我要用命令将clientPortAddress=172.17.81.110里面的IP地址修改为指定IP地址
后面还会在#server.3=172.17.81.113:2888:3888追加一条相同的数据,只是IP地址和前面的数据不同,
还有就是如何将#server.2=172.17.81.111:2888:3888这条数据给删除??

作者: cqranbihong   发布时间: 2011-09-22

clientPortAddress=172.17.81.110修改IP地址已经可以了,用下面的命令

sed -i 's/\(clientPortAddress=\)\S\S*/\1102.117.2.23/' test.txt

作者: cqranbihong   发布时间: 2011-09-22

用命令将clientPortAddress=172.17.81.110里面的IP地址修改为指定IP地址

C/C++ code
[owenliang@localhost bashShell.dir]$ cat sed2009年 | sed -r "s/^(clientPortAddress=)[0-9.]*/\1102.117.2.23/g"


#server.3=172.17.81.113:2888:3888追加一条相同的数据,只是IP地址和前面的数据不同

需求明确一点,sed是可以解决的.
C/C++ code

[owenliang@localhost bashShell.dir]$ line=`wc -l sed2009年 | cut -d " " -f 1`
[owenliang@localhost bashShell.dir]$ echo $line
19
[owenliang@localhost bashShell.dir]$ cat sed2009年 | sed "$line a #server.4=127.0.0.1"
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=5
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=2
# the port at which the clients will connect
clientPort=2181
maxClientCnxns=1000
dataDir=/hds/zk/zdatadir

dataLogDir=/hds/zk/zlogdir
clientPortAddress=172.17.81.110

#server.1=172.17.81.110:2888:3888
#server.2=172.17.81.111:2888:3888
#server.3=172.17.81.113:2888:3888
#server.4=127.0.0.1


还有就是如何将#server.2=172.17.81.111:2888:3888这条数据给删除??

C/C++ code
cat sed2009年 | sed -r "s/^#server.2.*//g"


作者: qq120848369   发布时间: 2011-09-22

[owenliang@localhost bashShell.dir]$ cat sed2009年 | sed "$ a #server.4=127.0.0.1" 

这个就行了... 我记起来有个$表示最后一行.

作者: qq120848369   发布时间: 2011-09-22