In Bash, How to Add a String After Each Line in a File

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

Bash: insert a line after each line

To add before each line:

$ echo "line1
line2
line3
line4
line5" | sed 's/^/peter\n/'
peter
line1
peter
line2
peter
line3
peter
line4
peter
line5

The sed script is fairly simple. It's a substitution command s/regex/replacement/. In this case, the regex is just ^, meaning "beginning of the line". And the replacement is peter plus \n, which is a newline special character.
So it reads like this:

before each line, insert "peter" plus a newline

To add after each line:

$ echo "line1
line2
line3
line4
line5" | sed 's/$/\npeter/'
line1
peter
line2
peter
line3
peter
line4
peter
line5
peter

Here, the script is similar except that the regex is $, which means "end of the line", and the newline is substituted before peter instead of after. It reads as follows:

after each line, add a newline and the word "peter"

Add a prefix string to beginning of each line


# If you want to edit the file in-place
sed -i -e 's/^/prefix/' file

# If you want to create a new file
sed -e 's/^/prefix/' file > file.new

If prefix contains /, you can use any other character not in prefix, or
escape the /, so the sed command becomes

's#^#/opt/workdir#'
# or
's/^/\/opt\/workdir/'

How to append a string at end of a specific line in a file in bash

Using sed and the pattern described:

sed '/192.168.1.2/s/$/ myalias/' file

Using sed and a specific line number:

sed '2s/$/ myalias/' file

How to add a string to every line in a file with Bash

You can use sed and you don't really need a pipe for this:

myval="1000"
sed "s/$/ $myval/" file > file.modified

for in-place editing:

sed -i "s/$/ $myval/" file
  • s/$/ $myval/ - places a space followed by the value of $myval at the end of each line

More about sed here: http://grymoire.com/Unix/Sed.html

How to insert a string into a line in a file after a key word in bash

A pure sed solution:

sed -r "s:(THEKEYWORD):\1 $(sed ':a;N;$!ba;s/\n/ /g' test.txt) :g" insert.txt

Where;

  • test.txt is the value to be inserted, $(sed ':a;N;$!ba;s/\n/ /g' test.txt) removes any newlines from the file, so it can be inserted on the same line
  • insert.txt the text file where THEKEYWORD exists

If you wish to replace the file, use the -i option;

sed -i -r "s:(THEKEYWORD):\1 $(gsed ':a;N;$!ba;s/\n/ /g' test.txt) :g" insert.txt

As @KamilCuk pointed out, using paste -sd ' ' test.txt could be used to remove the newlines, and insert the file;

sed -r "s:(THEKEYWORD):\1 $(paste -sd ' ' test.txt) :g" insert.txt

Terminal output + sed version

How to add a string from variables to each line in a file in BASH

If I understand you correctly, you can just save this string in a variable like so:

$ myvar=`cat filename2 | tr '\n' ' '`; sed -e "s/$/ $myvar/" -i filename

Basically, you store the value of the command inside myvar.
Then, you run sed and provide the contents of $myvar as a string for the s (substitute) command.

append string after each line from pipe line


./script.bin | sed "s/\$/ IP=$IP/" | tee -a file.log


Related Topics



Leave a reply



Submit