我只是环顾四周,但我没有find任何对我有用的东西。 我想插入一个新行(基本上是一个HTML表行)在其他行的顶部。
<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>
那么,有没有人可以build议我一个sed cmd将插入一个新的:
<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
1 some test lines here but not all lines contain nubers 3 and here is the last one
而且我们有一个bash变量$ADDED
和要添加的行内容
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/"
上面的例子在包含单词“all”的行后面加上一行代替
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/"
上面将搜索包含单词“all”的行并将其分割为“lines”之后。 结果:
1 some test lines here but not all lines ==This is the new line== contain nubers 3 and here is the last line
最后一件事。 这是不可能的解析与regural表达式的HTML,检查链接在sputnik的评论。
但是,这并不意味着不可能匹配 HTML文件的某些部分。 如果你知道你想匹配 (而不是解析) – 你也可以安全地使用HTML的正则表达式。 简单地说,这里的很多人不知道解析和匹配的区别。
所以,如果你的html文件具有众所周知的结构,比如你确定你的html会一直保持上述结构,那么你可以写下:
<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代码,我们只是匹配一些线模式。