Insert multiple lines into a file after specified pattern using shell script Insert multiple lines into a file after specified pattern using shell script shell shell

Insert multiple lines into a file after specified pattern using shell script


Another sed,

sed '/cdef/r add.txt' input.txt

input.txt:

abcdaccdcdeflineweb

add.txt:

line1line2line3line4

Test:

sat:~# sed '/cdef/r add.txt' input.txtabcdaccdcdefline1line2line3line4lineweb

If you want to apply the changes in input.txt file. Then, use -i with sed.

sed -i '/cdef/r add.txt' input.txt

If you want to use a regex as an expression you have to use the -E tag with sed.

sed -E '/RegexPattern/r add.txt' input.txt


Using GNU sed:

sed "/cdef/aline1\nline2\nline3\nline4" input.txt

If you started with:

abcdaccdcdeflineweb

this would produce:

abcdaccdcdefline1line2line3line4lineweb

If you want to save the changes to the file in-place, say:

sed -i "/cdef/aline1\nline2\nline3\nline4" input.txt


sed '/^cdef$/r'<(    echo "line1"    echo "line2"    echo "line3"    echo "line4") -i -- input.txt