I want to replace string from file with content from another file
File 1: T1.txt
TEXT1
TEXT2
TEXT3
TEXT4
File 2: T2.txt
Line with PLACEHOLDER
Line with PLACEHOLDER
Line with PLACEHOLDER
Line with PLACEHOLDER
I want to replace "PLACEHOLDER" string from "File 2" with all contents from "File 1", no matter if it create new file or change in existing file.
I tried:-
sed -i -e '/PLACEHOLDER/ r T2.sql' -e s/PLACEHOLDER// T1.sql
not working not sure why
This will insert a newline before the included text, and does not handle text that may follow PLACEHOLDER:
sed -e '/PLACEHOLDER/{ s///; rT1.txt
}' T2.txt
To avoid the additional newline and to handle PLACEHOLDER in the middle of a line, you could do:
perl -MFile::Slurp -pe 'BEGIN{$r=read_file("T1.txt")} s/PLACEHOLDER/$r/g' T2.txt
You probably (almost certainly) can make sed
do the inline substitution of a multiline value, but sed
really is line oriented and any such solution would require more gymnastics than using enhanced sed (aka, perl).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.