LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1156|回复: 8

求助: sed '/a/,/b/s/....'的问题

[复制链接]
发表于 2004-7-6 02:23:30 | 显示全部楼层 |阅读模式
以下是我要处理的一段代码:
          <tr height="25">
            <td width="130" align="right"><a href="http://www.game2me.com">www.game2me.com</a></td>
            <!-- g2m -->
            <td align="left" class="CSolution"> &nbsp; 2004 : 01 &nbsp; 02 &nbsp; 03 &nbsp; 04 &nbsp; 05 &nbsp; 06 &nbsp; <a href="game/200403/awstats.game.html" target=_blank>07</a> &nbsp;
            <td align="left" class="CSolution"> &nbsp; 2005 : 01 &nbsp; 02 &nbsp; 03 &nbsp; 04 &nbsp; 05 &nbsp; 06 &nbsp; <a href="game/200403/awstats.game.html" target=_blank>07</a> &nbsp;
            <!-- /g2m -->
            </td>   
          </tr>   
          <tr height="25">
            <td align="right" bgcolor="#f8f8f8"><a href="http://bn.game2me.com">bn.game2me.com</a></td>
            <!-- bn -->
            <td align="left" class="CSolution"> &nbsp; 2004 : 01 &nbsp; 02 &nbsp; 03 &nbsp; 04 &nbsp; 05 &nbsp; 06 &nbsp; <a href="http://gbbs.game2me.com/g2me/g2m/2004/07/awstats.g2m.html" target=_blank>07</a> &nbsp;
            <!-- /bn -->


要求在<!-- g2m -->与<!-- /g2m -->代码块中,在符合2004 || 2005 的行尾追加一段字串.


我想了一晚上没做出来.求高人指点.

谢谢!
发表于 2004-7-6 13:40:49 | 显示全部楼层

嗯.....

sed '/<!-- g2m -->/,/<!-- \/g2m -->/{/2004 :/s/$/some word you what to add at the end of 2004 :/;/2005 :/s/$/some word you what to add at the end of 2005 :/}' file

file就是你这个文件,另外两个some word .....就是你要添加的内容,sed好像不支持(2004|2005)这样的表示方法,之好这样来做了,我测试是可以的,你自己看看吧
 楼主| 发表于 2004-7-6 14:56:01 | 显示全部楼层
太好了,其他只是在2004或2005之间配匹而以。
 楼主| 发表于 2004-7-6 14:59:55 | 显示全部楼层
写的很好。谢谢chrisyan兄。
我要抄写N道。
sed '/<\!-- g2m -->/,/<!\-- \/g2m -->/{/2004 :/s/$/12345/}'

只要这样就很好了。
 楼主| 发表于 2004-7-6 15:22:35 | 显示全部楼层
有问题。
cat index.html | sed -n '/<\!-- g2m -->/,/<-- \/g2m -->/{/2004 :/s/$/tyfogame/p}'


这是我将chrisyan兄代码稍做改动来测试。
结果如下:
            <td align="left" class="CSolution"> &nbsp; 2004 : 01 &nbsp; 02 &nbsp; 03 &nbsp; 04 &nbsp; 05 &nbsp; 06 &nbsp; <a href="g2m/2004/07/awstats.g2m.html" target=_blank>07</a> &nbsp;tyfogame
            <td align="left" class="CSolution"> &nbsp; 2004 : 01 &nbsp; 02 &nbsp; 03 &nbsp; 04 &nbsp; 05 &nbsp; 06 &nbsp; <a href="bn/2004/07/awstats.bn.html" target=_blank>07</a> &nbsp;tyfogame


可以看出。sed检索的条件已超过指定的范围了
发表于 2004-7-6 16:19:05 | 显示全部楼层

超出范围了吗?

这两行确实都有2004 :阿,你说的超出范围是什么意思?
 楼主| 发表于 2004-7-6 20:34:50 | 显示全部楼层
是在<!-- g2m --> 与 <-- /g2m -->中配匹2004或者2005条件的进行修改啊!其他的不做改动。最后将所有的输出。

请大家帮帮忙。
 楼主| 发表于 2004-7-6 22:13:57 | 显示全部楼层

找到答案了.

<tr height="25">
<td width="130" align="right"><a href="http://www.game2me.com">www.game2me.com</a></td>
<!-- g2m -->
<td align="left" class="CSolution"> & 2004 : 01 & 02 & 03 & 04 & 05 & 06 & <a href="game/200403/awstats.game.html" target=_blank>07</a> &
<td align="left" class="CSolution"> & 2005 : 01 & 02 & 03 & 04 & 05 & 06 & <a href="game/200403/awstats.game.html" target=_blank>07</a> &
<!-- /g2m -->
</td>
</tr>
<tr height="25">
<td align="right" bgcolor="#f8f8f8"><a href="http://bn.game2me.com">bn.game2me.com</a></td>
<!-- bn -->
<td align="left" class="CSolution"> & 2004 : 01 & 02 & 03 & 04 & 05 & 06 & <a href="http://gbbs.game2me.com/g2me/g2m/2004/07/awstats.g2m.html" target=_blank>07</a> &
<!-- /bn -->


是经过反复测试,终于得出如下结果:
cat file | sed '/<\!-- g2m -->/,/<\!-- \/g2m -->/{/2004 :/{s/$/string/}}'> newfile


哈哈哈.感谢大家了.
发表于 2004-7-6 22:43:51 | 显示全部楼层
修改了一下
  1. cat file|sed '/<\!-- g2m -->/,/<\!-- \/g2m -->/{[color=red]/2004 :\|2005 :/[/color]{s/$/string/}}'> newfile
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表