Redirect tr output to sed

2019-07-13 17:04发布

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 ?

3条回答
一夜七次
2楼-- · 2019-07-13 17:40

This might work for you (GNU sed):

sed 's/foo/\L'"NEWSTRING"'/'g file
查看更多
放我归山
3楼-- · 2019-07-13 17:49
sed "s/foo/$(echo .. |tr ...)/g" file.txt
查看更多
一纸荒年 Trace。
4楼-- · 2019-07-13 17:50

Try using xargs, e.g.:

echo 'NEWSTRING' | tr '[:upper:]' '[:lower:]' | xargs -I '{}' sed 's/foo/{}/g' file.txt
查看更多
登录 后发表回答