how to use xargs with sed in search pattern

Neuquino picture Neuquino · Jan 18, 2013 · Viewed 42.1k times · Source

I need to use the output of a command as a search pattern in sed. I will make an example using echo, but assume that can be a more complicated command:

echo "some pattern" | xargs sed -i 's/{}/replacement/g' file.txt

That command doesn't work because "some pattern" has a whitespace, but I think that clearly illustrate my problem.

How can I make that command work?

Thanks in advance,

Answer

Weetu picture Weetu · Jan 18, 2013

You need to tell xargs what to replace with the -I switch - it doesn't seem to know about the {} automatically, at least in some versions.

echo "pattern" | xargs -I '{}' sed -i 's/{}/replacement/g' file.txt