bash sed:在某个位置插入一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15555781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sed: insert a line in a certain position
提问by iGio90
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.
我只是环顾四周,但没有找到任何适合我的东西。我想在其他行的顶部插入一个新行(基本上是一个 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>
So, is there anyone that can suggest me a sed cmd that will insert a new:
那么,有没有人可以建议我一个 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>
just below the HEADERS?
就在标题下面?
Thanks!
谢谢!
回答by jm666
So for the start, we have a file with the following lines, called datafile.txt
所以首先,我们有一个包含以下几行的文件,称为 datafile.txt
1 some test lines here
but not all lines contain nubers
3 and here is the last one
and we have one bash variable $ADDEDwith the line content what want add
我们有一个 bash 变量$ADDED,其中包含要添加的行内容
ADDED="==This is the new line=="
So, add line after the first line
所以,在第一行之后添加一行
ADDED="==This is the new line=="
< datafile.txt sed "1a \
$ADDED
"
the result:
结果:
1 some test lines here
==This is the new line==
but not all lines contain nubers
3 and here is the last line
Add line after all lines what are starts with a number
在所有以数字开头的行之后添加一行
< datafile.txt sed "/^[0-9]/a \
$ADDED
"
the result:
结果:
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==
Add line to the start, so insert before first line
在开头添加一行,因此在第一行之前插入
< datafile.txt sed "1i \
$ADDED
"
result
结果
==This is the new line==
1 some test lines here
but not all lines contain nubers
3 and here is the last line
You can "substitute" the end of the line for adding a new one
您可以“替换”行尾以添加新行
< datafile.txt sed "/all/s/$/\
$ADDED/"
the above example add line after the line what contains word "all" by substitution
上面的示例在包含单词“all”的行之后添加行替换
1 some test lines here
but not all lines contain nubers
==This is the new line==
3 and here is the last line
You can even split line and add between
您甚至可以分割线并在它们之间添加
< datafile.txt sed "/all/s/\(.*lines \)\(.*\)/\
$ADDED\
/"
the above will search for the line what contains the word "all" and split it after the word "lines". The result:
以上将搜索包含单词“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
Last thing. It is impossible to parsingHTML with regural expressions, check the link in sputnik's comment.
最后一件事。用正则表达式解析HTML是不可能的,请查看人造卫星评论中的链接。
BUT, that's not mean than it is impossible matchsome 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 surethan your html will the above structure all times, you can safely write:
因此,如果您的 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>
"
and you will get
你会得到
<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>
simply because we NOT PARSINGthe html code, we are only MATCHINGsome line patterns..
仅仅因为我们不解析html 代码,我们只匹配一些线条模式..

