Link to home
Start Free TrialLog in
Avatar of dirknibleck
dirknibleck

asked on

Shell script: How to append contents of one file to another WITHOUT line break

I am trying to append text from one file to another, with the hitch being that I need the text from the source file to begin at the end of the last line of the original file, not on the next line.

I am currently using:

cat source.txt >> origin.txt

Thoughts on how I can change this?

ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi dirknibleck,

This seems to work:
    perl -i -0pe 's/\n$//' origin.txt
    cat source.txt >>origin.txt

Notes:
- 1st line above removes the last new-line char from origin.txt.
- If the file is too big to fit in memory, then my method would not be advisable (but may still work, since virtual memory could be used).
- If there is a chance that origin.txt could contain a null (ASCII 0) char, change the "-0pe" to "-0777 -pe" (less concise, but more flexible).
- If you want to backup the old version of origin.txt as part of this, you could change the "-i" to something like "-i.bak" (which would backup to origin.txt.bak).
Avatar of dirknibleck
dirknibleck

ASKER

Thanks, that's exactly what I needed.
Hi dirknibleck,

Did you try my Perl solution?
Did it work?
Why did you not like it?