Bash: add string to the end of the file without line break Bash: add string to the end of the file without line break linux linux

Bash: add string to the end of the file without line break


You can use the -n parameter of echo. Like this:

$ touch a.txt$ echo -n "A" >> a.txt$ echo -n "B" >> a.txt$ echo -n "C" >> a.txt$ cat a.txtABC

EDIT: Aha, you already had a file containing string and newline. Well, I'll leave this here anyway, might we useful for someone.


Just use printf instead, since it does not print the new line as default:

printf "final line" >> file

Test

Let's create a file and then add an extra line without a trailing new line. Note I use cat -vet to see the new lines.

$ seq 2 > file$ cat -vet file1$2$$ printf "the end" >> file$ cat -vet file1$2$the end


sed '$s/$/yourText2/' list.txt > _list.txt_ && mv -- _list.txt_ list.txt

If your sed implementation supports the -i option, you could use:

sed -i.bck '$s/$/yourText2/' list.txt

With the second solution you'll have a backup too (with first you'll need to do it manually).

Alternatively:

ex -sc 's/$/yourText2/|w|q' list.txt 

or

perl -i.bck -pe's/$/yourText2/ if eof' list.txt