【shell脚本】使用sed快速批量替换文件中的字段

使用sed快速批量替换文件夹内的文件中的某个字段

sed用法总结

sed高级用法:模式空间(pattern space)和保持空间(hold space)

sed删除的用法

sed命令的a、c、q命令

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]# 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值