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

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


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


I prefer using awk.If there is only one column, use $0, else replace it with the last column.

One way,

awk '{print $0, "string to append after each line"}' file > new_file

or this,

awk '$0=$0"string to append after each line"' file > new_file


I prefer echo. using pure bash:

cat file | while read line; do echo ${line}$string; done