dingo_d
dingo_d

Reputation: 11670

Using output of a pipe in sed for replacing in file

I'm trying to use an output of a pipe as a variable that I'll pass to sed to search and replace in file.

The contents of my .env file is

TEST=bla
TEST_2=blabla

and I want to replace %TEST% in my config.txt file

This is some fake config file. The value of test variable is %TEST%.

After replacement, this statement should be

This is some fake config file. The value of test variable is bla.

I've tried

grep 'TEST=' .env | sed 's/^.*=//' |  sed -i '' 's|%TEST%|$1|g' config.txt

But the result is

This is some fake config file. The value of test variable is $1.

How do I capture and use the output of a previous pipe in sed?

Upvotes: 1

Views: 4385

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

I suggest first extracting the text you need to replace with into a variable and then replace with it:

repl="$(sed -n 's/^TEST=\(.*\)/\1/p' .env)"
sed -i '' "s|%TEST%|$repl|g" config.txt

Or as an one-liner

sed -i '' "s|%TEST%|$(sed -n 's/^TEST=\(.*\)/\1/p' .env)|g" config.txt

See the online demo

Here, sed -n 's/^TEST=\(.*\)/\1/p' .env is extracting the text found on a line that starts with TEST= and is right after TEST= into repl.

Note that "s|%TEST%|$repl|g" is within double quotation marks, that allows variable expansion.

Also, if the string you want to replace with contains various "special chars" you may need to escape it, see Is it possible to escape regex metacharacters reliably with sed.

Upvotes: 2

Related Questions