使用sed加入html行

时间:2015-05-13 16:35:38

标签: html linux sed

我在html td标签中有一个url,分为多行,我需要加入。

示例:

<div class="a">
<div><img src="https://lh6.googleusercontent.com/-duniA1A1C0c/UXWqPdzXeCI/AAAAAAAAKio/Mj3DIbKXV7s/w126-h126-p/HD_beautiful_nature_landsacpe_2.jpg" /></div>
<div><img src="https://lh5.googleusercontent.com/-IdeJ_2r_7hk/UJ9DtKJx0TI/AAAAAAAAFnw/sZ8FQ-8lGjE/w126-h126-p/fabulous-nature-scenery-25-photos-12.jpg" /></div>
<div><img src="https://lh6.googleusercontent.com/--0CjkmkSU_8/U-DkyWhcX5I/AAAAAAAAIf8/hvpgW5UCnb0/w126-h126-p/Nature-0057.jpg" /></div>
<div><img src="https://lh3.googleusercontent.com/-etqb2vA6RFg/T0Jmv2PtprI/AAAAAAAAEmY/Rz30YnMNX7E/w126-h126-p/Japanese+Nature+Picture+%2818%29.jpg" /></div>
<div><img src="https://lh6.googleusercontent.com/-Hf-iWqukw-w/UJ9Ds7xTYQI/AAAAAAAAFns/wNQ-2K9no-Y/w126-h126-p/fabulous-nature-scenery-25-photos-11.jpg" /></div>
</div>
<div class="b">
<div><img src="https://lh6.googleusercontent.com/-duniA1A1C0c/UXWqPdzXeCI/AAAAAAAAKio/Mj3DIbKXV7s/w126-h126-p/HD_beautiful_nature_landsacpe_2.jpg" /></div>
<div><img src="https://lh5.googleusercontent.com/-IdeJ_2r_7hk/UJ9DtKJx0TI/AAAAAAAAFnw/sZ8FQ-8lGjE/w126-h126-p/fabulous-nature-scenery-25-photos-12.jpg" /></div>
<div><img src="https://lh6.googleusercontent.com/--0CjkmkSU_8/U-DkyWhcX5I/AAAAAAAAIf8/hvpgW5UCnb0/w126-h126-p/Nature-0057.jpg" /></div>
<div><img src="https://lh3.googleusercontent.com/-etqb2vA6RFg/T0Jmv2PtprI/AAAAAAAAEmY/Rz30YnMNX7E/w126-h126-p/Japanese+Nature+Picture+%2818%29.jpg" /></div>
<div><img src="https://lh6.googleusercontent.com/-Hf-iWqukw-w/UJ9Ds7xTYQI/AAAAAAAAFns/wNQ-2K9no-Y/w126-h126-p/fabulous-nature-scenery-25-photos-11.jpg" /></div>
</div>

我需要加入一切看起来像这样:

<td>
<a
href="fix_detail.jsp?fix=23056&list=yes">PICTURE
ROCKS</a>&nbsp;
</td>

我更愿意实现一个sed循环,但我不明白如何循环搜索结束&#34; td&#34;标签

<a href="fix_detail.jsp?fix=23056&list=yes">PICTURE ROCKS</a>&nbsp;</td>

2 个答案:

答案 0 :(得分:2)

保持理智,只需使用awk:

$ cat file
foo
<td>
<a
href="fix_detail.jsp?fix=23056&list=yes">PICTURE
ROCKS</a>&nbsp;
</td>
bar

$ awk '/<td>/{f=1} /<\/td>/{f=0} {ORS=(f?" ":"\n"); print}' file
foo
<td> <a href="fix_detail.jsp?fix=23056&list=yes">PICTURE ROCKS</a>&nbsp; </td>
bar
在20世纪70年代中期发明awk时,所有神秘的sed构造在多行中做事都变得过时了,它们今天只用作脑筋急转弯。

答案 1 :(得分:0)

这是一个sed one liner

sed '/<td>/{:loop;/<\/td>/!{N;s/\n/ /}; t loop}' file

输出

<td> <a href="fix_detail.jsp?fix=23056&list=yes">PICTURE ROCKS</a>&nbsp; </td>

首先匹配“&lt; td&gt;”,然后它会启动一个追加行的循环,并在到达“&lt; / td&gt;”时终止标签