Chinaunix

标题: 求助: 关于替换文件中两个关键字之间的内容 [打印本页]

作者: smarter_feng    时间: 2012-12-07 15:08
标题: 求助: 关于替换文件中两个关键字之间的内容
例如: 文件内容为:
  1. before  hello world  after!
  2. goodbay!
复制代码
我想把before 和 after 之间的内容替换为: hi, chinaunix, 请问如何写脚本实现。 谢谢!
作者: dn833    时间: 2012-12-07 16:39
  1. sed -r '/^before.*after!$/ s/(^before).*(after!$)/\1 hi,chinaunix \2/'
复制代码

作者: smarter_feng    时间: 2012-12-07 17:07
谢谢大神! 我去试验下哈!
dn833 发表于 2012-12-07 16:39

作者: smarter_feng    时间: 2012-12-07 17:17
好像不行啊, 没有替换成功, 不知道为什么
dn833 发表于 2012-12-07 16:39

作者: dn833    时间: 2012-12-07 17:30
copy操作过程上来看看
作者: smarter_feng    时间: 2012-12-07 17:33
  1. #!/bin/bash

  2. IP=$1
  3. sed -r "/^eth0.*netmask!$/ s/(^eth0).*(netmask!$)/\1 ${IP} \2/" $2

  4. sed -r '/^eth0.*netmask!$/ s/(^eth0).*(netmask!$)/\1 hi, chinaunix \2/' $2

  5. exit 0
复制代码
dn833 发表于 2012-12-07 17:30
copy操作过程上来看看

作者: Shell_HAT    时间: 2012-12-07 18:59
回复 6# smarter_feng


    执行过程和结果贴出来看看
作者: kernel69    时间: 2012-12-07 22:36
试一下这个
  1. echo 'before  hello world  after!
  2. goodbay!'|perl -pe 's#before .* after#before hi, chinaunix after#'
复制代码
回复 1# smarter_feng


   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2