replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this?

e.g:

Say file1.txt contains:

today is monday
the 22 of
NOVEMBER
2010

and file2.txt contains:

the
11th
month
of

How do i replace the word NOVEMBER with

the
11th
month
of

so output file is:

today is monday
the 22 of
the
11th
month
of
2010

Thanks for your help!

sed  '/NOVEMBER/r file2.txt' file1.txt |sed '/NOVEMBER/d'
1 Like

thanks! this works well but is there a way to just replace NOVEMBER in file1.txt rather than just output to terminal??

if your sed support -i option, you can update the file directly, otherwise, export to temp file, and rename it back.

1 Like

Hi Sir!

I have a similar scenario. I have an xml file which I compiled in unix and i send it out as an attachment which opens with excel. Now there are certain keywords (kw01)in the xml file which I wanted to replace with the contents of txt files having multiple lines.

So i tried your code above with this command:
sed '/kw01/r fatal_alerts.txt' template.xml | sed '/kw01/d' > test.xml

now when I sent out the output file 'test.xml' and open it via excel, the cell where 'kw01' is located is not replaced with the contents of 'fatal_alerts.txt'.

Is there a possible way to do this sir? Need your help badly. :frowning:

#!/bin/bash
# tested with bash 4
file2=$(<file2)
while read -r line
do
    case "$line" in
        *NOVEMBER*)
        line="$file2"
        ;;
    esac
    echo "$line"
done < file1 > t && mv t file1