sed高级用法:模式空间(pattern space)和保持空间(hold space)
1.替换内容
sed -i "s/192.168.118.231/192.168.118.138/g" /usr/local/app/tars/tarsnode/data/tars.tarsAdminRegistry/conf/tars.tarsAdminRegistry.config.conf
sed -i "s/192.168.118.231/192.168.118.138/g" /usr/local/app/tars/tarsnode/data/tars.tarsnotify/conf/tars.tarsnotify.config.conf
sed -i "s/192.168.118.231/192.168.118.138/g" /usr/local/app/tars/tarsnode/data/tars.tarsproperty/conf/tars.tarsproperty.config.conf
sed -i "s/192.168.118.231/192.168.118.138/g" /usr/local/app/tars/tarsnode/data/tars.tarsqueryproperty/conf/tars.tarsqueryproperty.config.conf
sed -i "s/192.168.118.231/192.168.118.138/g" /usr/local/app/tars/tarsnode/data/tars.tarsquerystat/conf/tars.tarsquerystat.config.conf
sed -i "s/192.168.118.231/192.168.118.138/g" /usr/local/app/tars/tarsnode/data/tars.tarsstat/conf/tars.tarsstat.config.conf
sed -i "s/192.168.118.231/192.168.118.138/g" /usr/local/app/tars/tarsnode/data/tars.tarsregistry/conf/tars.tarsregistry.config.conf
sed -i "s/192.168.118.231/192.168.118.138/g" /usr/local/app/tars/tarsnode/data/tars.tarsconfig/conf/tars.tarsconfig.config.conf
2.把所有的行归为一行
[root@localhost ~]# sed ':a;N;$!ba;s/\n/ /g' file.txt
wtmp begins Mon Feb 25 14:26:08 2014 192.168.0.1 162.12.0.123 this is the last line
3.用【\"】替换掉【"】
sed -i 's/"/\\"/g' filename
sed -i 's:\":\\\":g' filename
4.我的一个例子
[muten003@wknode unitTestTool]$ more removeLineBreak.sh
sed ':a;N;$!ba;s/\n/ /g' file.txt >> new.txt
sed -i "s/ //g" new.txt
sed -i "s/\"/\/\"/g" new.txt
第一行实现所有行都放到一行并将结果写到new.txt中
第二行实现文件new.txt中的空格都去除
第三行实现new.txt文件中的【"】都转成【\"】
5.sed中c的作用
[root@localhost linux_x86]# sed -f s data
usudsudysudu
cc is a girl
zoer is naughty
[root@localhost linux_x86]#
[root@localhost linux_x86]#
[root@localhost linux_x86]# more s
/^nau/c\usudsudysudu
[root@localhost linux_x86]#
[root@localhost linux_x86]# more data
naughty is a boy
cc is a girl
zoer is naughty
观察这个空行使用【\】实现的:
[root@localhost linux_x86]# sed -f s data
usudsudysudu
cc is a girl
zoer is naughty
[root@localhost linux_x86]#
[root@localhost linux_x86]# more s
/^nau/c\usudsudysudu\
[root@localhost linux_x86]#
[root@localhost linux_x86]#
[root@localhost linux_x86]# more data
naughty is a boy
cc is a girl
zoer is naughty
[root@localhost linux_x86]#
[root@localhost linux_x86]# more s
/^nau/c\FROM 127.0.0.1\/library\/redis:v3.0.0
[root@localhost linux_x86]#
[root@localhost linux_x86]#
[root@localhost linux_x86]# more data
naughty is a boy
cc is a girl
zoer is naughty
[root@localhost linux_x86]#
[root@localhost linux_x86]# sed -f s data
FROM 127.0.0.1/library/redis:v3.0.0
cc is a girl
zoer is naughty
[root@localhost linux_x86]#