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

1
  • 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
    Commented Mar 26, 2015 at 8:19

1 Answer 1

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
5
  • And if I want to add content of a text file instead of ddd?
    – antiks
    Commented Mar 26, 2015 at 7:28
  • Answer extended to your new question (and more variants).
    – Janis
    Commented Mar 26, 2015 at 8: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
    Commented Mar 26, 2015 at 9:25
  • There's a spurious quote in your command; try cat sed '$s/$/ '$(< ff)'/' f >> fff instead.
    – Janis
    Commented Mar 26, 2015 at 18:37
  • Remove the cat from my previous comment (I copied it from your code without noticing).
    – Janis
    Commented Mar 26, 2015 at 22:17

Not the answer you're looking for? Browse other questions tagged .