Execute a cat command within a sed command in Linux

amar picture amar · Jun 10, 2013 · Viewed 22.8k times · Source

I have a file.txt that has some content. I want to search for a string in file1.txt, if that string is matched I want to replace that string with the content of the file.txt. How can I achieve this?

I have tried using sed:

sed -e 's/%d/cat file.txt/g' file1.txt

This is searching for the matching string in the file1.txt and replacing that with the string cat file.txt, but I want contents of file.txt instead.

Answer

Ivan Kolmychek picture Ivan Kolmychek · Jun 10, 2013

How about saving the content of the file in the variable before inserting it into sed string?

$content=`cat file.txt`; sed "s/%d/${content}/g file1.txt"