Results 1 to 2 of 2
Hi,
Easy question hopefully. I have a file with the contents
foo
foo
bar
bar
bar
foo
foo
And I want to add a line 'a pointless line' after the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 10-24-2009 #1Just Joined!
- Join Date
- Oct 2009
- Posts
- 4
sed question
Hi,
Easy question hopefully. I have a file with the contents
foo
foo
bar
bar
bar
foo
foo
And I want to add a line 'a pointless line' after the last line containing the string bar. How would I do this with sed ?
I managed :
sed ' /bar/ a\
a pointless line' < ./foobar.txt
which prints :
foo
foo
bar
a pointless line
bar
a pointless line
bar
a pointless line
foo
foo
So not quite the desired effect as I only wanted it to print:
foo
foo
bar
bar
bar
a pointless line
foo
foo
- 10-30-2009 #2Linux Newbie
- Join Date
- Jan 2008
- Location
- Canada
- Posts
- 109
Hi ed91270
Hi. It would appear to me that if you want to parse a file and enter a line, as you mention after the last instance of <bar>, you must parse the file twice. Once to locate the last line where the <bar> occurs, possible by line number, and second pass insert your <line> at Line Number + one. Not sure how else you could know where the last instance of <bar> is located. Not sure this helps much. Cheers...
Robert


Reply With Quote
