PDA

View Full Version : sed: append text to a line before the end line?



PryGuy
August 21st, 2009, 04:04 PM
Good day to you all!
I have a question, I have to append a line to a line prior to end, so the text would look:
Old Line
Old Line
Old Line
NEW LINE!
Old LineIt's not as simple as I figured... Thanks in advance!

unutbu
August 21st, 2009, 04:32 PM
awk '{new=$0; print old; old=new}END{print "NEW LINE!"; print old}' file

PryGuy
August 21st, 2009, 05:48 PM
Thank you, unutbu! You help me for the second time! :)
But is it impossible for the sed almighty? It's such an easy task... :(

DaithiF
August 21st, 2009, 05:57 PM
Hi,
In sed:
sed '$iNew Line!' somefile

PryGuy
August 21st, 2009, 06:15 PM
Hi,
In sed:
sed '$iNew Line!' somefileThis will add line to the very end as far as I get it...

DaithiF
August 21st, 2009, 06:22 PM
Hi, no $i inserts before the last line. $a would append after the last line.

So:

xxx@xxx:~/tmp$ cat testfile
firstline
second line
third line
xxx@xxx:~/tmp$ sed '$iNew Line!' testfile
firstline
second line
New Line!
third line

PryGuy
August 21st, 2009, 06:51 PM
Rule, Britain!!!
P.S. If you only knew how I miss London...

Albert Net
February 9th, 2010, 11:26 AM
Re:unutbu


awk '{new=$0; print old; old=new}END{print "NEW LINE!"; print old}' file
This code quite tricky. Unfortunately, there is one bug that it insert one blank line at the first line of the file, for "old" is empty when gawk is processing the first line.


sed '$i \
yourinsertedtext' fileThis one is the one that should be used, for it is rather simple.