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.
You can do: sed "s/%d/$(cat file. txt)/g" file1.
sed can be used with other CLI tools through pipelines. Since sed can read an input stream, CLI tools can pipe data to it. Similarly, since sed writes to stdout, it can pipe its output to other commands. The sed and awk examples illustrate this.
Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.
Given original.txt
with content:
this is some text that needs replacement
and replacement.txt
with content:
no changes
I'm not sure what your replacement criteria is, but lets say I want to replace all occurrences of the word replacement
in the original file, you may replace the content of the original text with the content of the other file using:
> sed -e 's/replacement/'"`cat replacement.txt`"'/g' original.txt
this is some text that needs no changes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With