Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a cat command within a sed command in Linux

Tags:

linux

sed

cat

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.

like image 444
amar Avatar asked Jun 10 '13 05:06

amar


People also ask

How do you use the cat command in sed?

You can do: sed "s/%d/$(cat file. txt)/g" file1.

Can you pipe to sed?

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.

How do you use sed command to replace a string in a file?

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.


1 Answers

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
like image 182
skofgar Avatar answered Nov 07 '22 06:11

skofgar