问题描述
我只是环顾四周,但我没有发现任何对我的作品。
我想在其他行的上方插入新行(基本上是一个HTML表行)。
i was just looking around but i didn't find anything that works for me.I would like to insert a new line (basically an html table row) on top of the other rows.
<table id="tfhover" class="tftable" border="1">
<tr><th>HEADER1</th><th>HEADER2</th><th>HEADER3</th><th>HEADER4</th></tr>
<tr><td>Row:1 Cell:1</td><td>Row:1 Cell:2</td><td>Row:1 Cell:3</td><td>Row:1 Cell:4</td></tr>
</table>
那么,有没有任何人可以建议我一个sed CMD,将插入一个新的:
So, is there anyone that can suggest me a sed cmd that will insert a new:
<tr><td>Row:1 Cell:1</td><td>Row:1 Cell:2</td><td>Row:1 Cell:3</td><td>Row:1 Cell:4</td>
只是标题下面?
谢谢!
推荐答案
因此,对于一开始,我们有以下行,称为文件 datafile.txt
So for the start, we have a file with the following lines, called datafile.txt
1 some test lines here
but not all lines contain nubers
3 and here is the last one
和我们有一个bash的变量 $ ADDED
什么要添加的行内容
and we have one bash variable $ADDED
with the line content what want add
ADDED="==This is the new line=="
所以,第一行后添加行
ADDED="==This is the new line=="
< datafile.txt sed "1a \\
$ADDED
"
结果:
1 some test lines here
==This is the new line==
but not all lines contain nubers
3 and here is the last line
什么是数字开始的所有行后添加行
< datafile.txt sed "/^[0-9]/a \\
$ADDED
"
结果:
1 some test lines here
==This is the new line==
but not all lines contain nubers
3 and here is the last line
==This is the new line==
添加行的开始,所以插入第一行之前
< datafile.txt sed "1i \\
$ADDED
"
结果
==This is the new line==
1 some test lines here
but not all lines contain nubers
3 and here is the last line
您可以替代行了添加一个新的结束
< datafile.txt sed "/all/s/$/\\
$ADDED/"
上面的例子中包含什么字行之后添加行都通过置换
the above example add line after the line what contains word "all" by substitution
1 some test lines here
but not all lines contain nubers
==This is the new line==
3 and here is the last line
你甚至可以分割线和
之间添加< datafile.txt sed "/all/s/\(.*lines \)\(.*\)/\1\\
$ADDED\\
\2/"
以上会搜索什么包含了所有一词,并在线后,分割线。其结果是:
the above will search for the line what contains the word "all" and split it after the word "lines". The result:
1 some test lines here
but not all lines
==This is the new line==
contain nubers
3 and here is the last line
最后一件事。这是不可能的解析 HTML与regural前pressions,检查人造卫星的评论的链接。
Last thing. It is impossible to parsing HTML with regural expressions, check the link in sputnik's comment.
但是,这并不意味着比这是不可能的匹配的HTML文件的某些部分。如果你知道你想要什么的匹配(而不是解析) - 你可以放心地使用常规的前pression为HTML了。简单地说,许多人在这里不知道解析和匹配之间的区别。
BUT, that's not mean than it is impossible match some parts of HTML files. If you know what you want match (and not parse) - you can safely use regular expression for HTML too. Simply, many peoples here don't know the difference between parsing and matching.
所以,如果你的HTML文件已众所周知的结构,例如您的确定的比你的HTML将上述结构中的所有时间,你可以放心地写:
So, if your html files has well known structure, e.g. you are sure than your html will the above structure all times, you can safely write:
<your_file.html sed "/^<tr><th>/a \\
<tr><td>new Row:1 Cell:1</td><td>Row:1 Cell:2</td><td>Row:1 Cell:3</td><td>Row:1 Cell:4</td>
"
和您将获得
<table id="tfhover" class="tftable" border="1">
<tr><th>HEADER1</th><th>HEADER2</th><th>HEADER3</th><th>HEADER4</th></tr>
<tr><td>new Row:1 Cell:1</td><td>Row:1 Cell:2</td><td>Row:1 Cell:3</td><td>Row:1 Cell:4</td>
<tr><td>Row:1 Cell:1</td><td>Row:1 Cell:2</td><td>Row:1 Cell:3</td><td>Row:1 Cell:4</td></tr>
</table>
只是因为我们的不解析的的HTML code,我们只的匹配的一些线条图案。
simply because we NOT PARSING the html code, we are only MATCHING some line patterns..
这篇关于SED:在某个位置插入一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!