sed的几个使用技巧

#sed 注释行的 4 种方法

#server 0.centos.pool.ntp.org这段字符串前插入#,而不是这一行,所以这段字符串如果不是在这一行的开头,那#就是插在中间的,起不到注释的效果

sed -i 's/server 0.centos.pool.ntp.org/#&/' /etc/ntp.conf

 #在匹配到server 1.centos.pool.ntp.org行前插入#

sed -i '/server 1.centos.pool.ntp.org/s/^/#/' /etc/ntp.conf

#server 2.centos.pool.ntp.org iburst替换为#server 2.centos.pool.ntp.org iburst

sed -i 's/server 2.centos.pool.ntp.org iburst/#server 2.centos.pool.ntp.org iburst/' /etc/ntp.conf

#只将第一个'charly'替换为'charly-->',而非全文替换

sed -i '0,/charly/s//charly-->/' /build/web/webapps/latest/WEB-INF/applicationContext-job.xml

#替换以server 3.centos.pool.ntp.org iburst开头的;&的意思是匹配任意字符(就是说未知数,啥都行)

sed -i 's/^server 3.centos.pool.ntp.org iburst/#&/' /etc/ntp.conf

 #配置上游时间服务器为本地的ntpd Server服务器

sed -i '25a\server 10.0.13.100' /etc/ntp.conf

#配置允许上游时间服务器主动修改本机的时间

sed -i '26a\restrict 10.0.13.100 nomodify notrap noquery'  

 #sed取消注释行

sed -i 's/^#server0.centos.pool.ntp.orgiburstserver0.centos.pool.ntp.orgiburst/\1/' /etc/ntp.conf

sed -i 's/^#server1.centos.pool.ntp.orgiburstserver1.centos.pool.ntp.orgiburst/\1/' /etc/ntp.conf 

   ##\1的意思 就类似于   前面的 (bbb\) \1就是复制这个位置的内容  如果有 第二个 那么久\2就是复制第二个位置的内容

sed -i 's/^#server 2.centos.pool.ntp.org iburst/server 2.centos.pool.ntp.org/' /etc/ntp.conf

sed -i 's/^#server3.centos.pool.ntp.orgiburstserver3.centos.pool.ntp.orgiburst/\1/' /etc/ntp.conf

#hosts文件中的ip地址替换为ec2-000-111-222-333..compute-1.amazonaws.com格式

sed -E 's/([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/ec2-\1-\2-\3-\4.compute-1.amazonaws.com/g' hosts

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值