In Bash, how do I add a string after each line in a file?

Jason Volkoz picture Jason Volkoz · May 19, 2010 · Viewed 154k times · Source

How do I add a string after each line in a file using bash? Can it be done using the sed command, if so how?

Answer

Tom DeGisi picture Tom DeGisi · May 19, 2010

If your sed allows in place editing via the -i parameter:

sed -e 's/$/string after each line/' -i filename

If not, you have to make a temporary file:

typeset TMP_FILE=$( mktemp )

touch "${TMP_FILE}"
cp -p filename "${TMP_FILE}"
sed -e 's/$/string after each line/' "${TMP_FILE}" > filename