1

If I have this document

aaa  
bbb  
ccc  

And I want to add " ddd" on the last line. My document should look like this:

aaa  
bbb  
ccc ddd  

How can I do this? If I prefer awk or sed but i'm open to your proposals. Thanks

muru
  • 72,889
antiks
  • 419
  • may by you should tell all the problem at once, you want to add a fixed string, a file, part of a file ? in last line ? add many lines ;; ? – Archemar Mar 26 '15 at 08:19

1 Answers1

2

In this case sed is the simplest. To add text to the last line of file f:

sed '$s/$/ ddd/' f

To append contents of a file ff after the last line of file f, one of:

sed '$r ff' f

cat ff >> f

To append contents of a file ff (with only one line of data) to the last line of file f:

sed '$s/$/ '$(< ff)'/' f
Janis
  • 14,222
  • And if I want to add content of a text file instead of ddd? – antiks Mar 26 '15 at 07:28
  • Answer extended to your new question (and more variants). – Janis Mar 26 '15 at 08:25
  • Actualy is not working. The last command display me what I want but I want to save the result in a file. Something like this 'cat sed '$s/$/ '$(< ff)'/' f' >> fff – antiks Mar 26 '15 at 09:25
  • There's a spurious quote in your command; try cat sed '$s/$/ '$(< ff)'/' f >> fff instead. – Janis Mar 26 '15 at 18:37
  • Remove the cat from my previous comment (I copied it from your code without noticing). – Janis Mar 26 '15 at 22:17