Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

linux - sed: insert a line in a certain position

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>

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>

just below the HEADERS?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

and we have one bash variable $ADDED with the line content what want add

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

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 )(.*)/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

Last thing. It is impossible to parsing HTML with regural expressions, check the link in sputnik's comment.

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.

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>
"

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 PARSING the html code, we are only MATCHING some line patterns..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...