Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I append a variable text to last line of file via command line?

Tags:

bash

sed

could you please tell me how I (a Linux-User) can add text to the last line of a text-file?

I have this so far:

APPEND='Some/Path which is/variable'
sed '${s/$/$APPEND/}' test.txt

It works, but $APPEND is added insted of the content of the variable. I know the reason for this is the singe quote (') I used for sed. But when I simply replace ' by ", no text gets added to the file.

Do you know a solution for this? I don't insist on using sed, it's only the first command line tool that came in my mind. You may use every standard command line program you like.

edit: I've just tried this:

$ sed '${s/$/'"$APPEND/}" test.txt
sed: -e Ausdruck #1, Zeichen 11: Unbekannte Option für `s'
like image 804
Martin Thoma Avatar asked Jul 08 '11 13:07

Martin Thoma


People also ask

Which command can be used to append some text at the end of some file?

Append Text Using >> Operator For example, you can use the echo command to append the text to the end of the file as shown. Alternatively, you can use the printf command (do not forget to use \n character to add the next line).

How do you add text to a file in command prompt?

But, if you would like to add some text to the file, you can type in after this, like this: cat >> new. txt This is some text in the file from command line. To stop editing and saving in the file, simply type CTRL+C, it will create, save and exit the file.


1 Answers

echo "$(cat $FILE)$APPEND" > $FILE

This was what I needed.

like image 143
Martin Thoma Avatar answered Oct 13 '22 01:10

Martin Thoma