PapelPincel
PapelPincel

Reputation: 4393

Redirect tr output to sed

I need to replace a string in a file with another string, but before the replacement I need to lowercase the new string before passing it to sed.

echo 'NEWSTRING' | tr '[:upper:]' '[:lower:]' | sed 's/foo/(my tr output in lowercase)/g' file.txt

My question is, How we could pass the replacement string as a parameter ?

Upvotes: 2

Views: 1012

Answers (3)

potong
potong

Reputation: 58483

This might work for you (GNU sed):

sed 's/foo/\L'"NEWSTRING"'/'g file

Upvotes: 3

etuardu
etuardu

Reputation: 5536

Try using xargs, e.g.:

echo 'NEWSTRING' | tr '[:upper:]' '[:lower:]' | xargs -I '{}' sed 's/foo/{}/g' file.txt

Upvotes: 4

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143229

sed "s/foo/$(echo .. |tr ...)/g" file.txt

Upvotes: 4

Related Questions