Insert multiple lines into a file after specified pattern using shell script
Another sed
,
sed '/cdef/r add.txt' input.txt
input.txt:
abcd
accd
cdef
line
web
add.txt:
line1
line2
line3
line4
Test:
sat:~# sed '/cdef/r add.txt' input.txt
abcd
accd
cdef
line1
line2
line3
line4
line
web
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:
abcd
accd
cdef
line
web
this would produce:
abcd
accd
cdef
line1
line2
line3
line4
line
web
If you want to save the changes to the file in-place, say:
sed -i "/cdef/aline1\nline2\nline3\nline4" input.txt