【发布时间】:2017-03-13 22:09:15
【问题描述】:
刚刚学习 sed,我感觉自己离做我想做的事情越来越近了,只是遗漏了一些明显的东西。
目标是在一个 html 表中获取一堆 <tr>...</tr>s 并将其附加到另一个页面中的单个表中。所以我想获取初始文件,在我第一次使用<tr> 时剥离所有内容,以及从</table> 开始的所有内容,然后将其插入到另一个文件中</table> 的上方。所以就像下面一样,除了<tr> 和</tr> 在自己的线路上,如果重要的话。
Input File: Target File:
<html><body> <html><body>
<p>Whatever...</p> <p>Other whatever...</p>
<table> <table>
<tr><td>4</td></tr> <thead>
<tr><td>5</td></tr> <tr><th>#</th></tr>
<tr><td>6</td></tr> </thead>
</table> <tbody>
</body></html> <tr><td>1</td></tr>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
</tbody>
</table>
</body></html>
变成:
Input file Target File:
doesn't matter. <html><body>
<p>Other whatever...</p>
<table>
<thead>
<tr><th>#</th></tr>
</thead>
<tbody>
<tr><td>1</td></tr>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
<tr><td>4</td></tr>
<tr><td>5</td></tr>
<tr><td>6</td></tr>
</tbody>
</table>
</body></html>
这是我尝试使用的代码:
#!/bin/bash
#$1 is the first parameter and $2 is the second parameter being passed when calling the script. The variable filename will be used to refer to this.
input=$1
inserttarget=$2
sed -e '/\<\/thead\>,$input' $input
sed -e '/\<\/table\>,$input' $input
sed -n -i -e '\<\/tbody\>/r' $inserttarget -e 1x -e '2,${x;p}' -e '${x;p}' $input
很确定这很简单,只是把表达搞砸了。谁能直截了当?
【问题讨论】: