Run cat command with sed command in linux

I have file.txt that has some content. I want to find a line in file1.txt , if this line matches, I want to replace this line with the contents of file.txt . How can I achieve this?

I tried using sed :

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

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

+6
source share
2 answers

How to save the contents of a file in a variable before inserting it into the sed string?

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

You can read the file using sed with the r command. However, this is a linear operation that may not be what you are after.

 sed '/%d/r file1.txt' 

Reading takes place at the end of the per-line cycle.

0
source

Source: https://habr.com/ru/post/946920/


All Articles