使用一个sed命令进行多次替换 [英] Multiple replacements with one sed command
问题描述
我想知道如何使用Mac OSX
中的单个sed
语句进行多次查找/替换.我可以在Ubuntu
中执行此操作,但是由于OSX
的BSD
性质,因此必须对命令进行一些更改.
I'm wondering how I can do a multiple find/replace using a single sed
statment in Mac OSX
. I'm able to do this in Ubuntu
but because of the BSD
nature of OSX
, the command must be slightly altered.
因此,给定一个包含字符串的文件:
So, given a file with the string:
"Red Blue Red Blue Black Blue Red Blue Red"
我想运行一个sed语句,输出结果:
I want to run a sed statement that results in the output:
"Green Yellow Green Yellow Black Yellow Green Yellow Green"
我的两个带有合格find
color1="Green"
color2="Yellow"
find . -type f -exec sed -i '' s/Red/$color1/g {} \;
find . -type f -exec sed -i '' s/Blue/$color2/g {} \;
I've tried several combinations of semicolons and slashes, and looked at Apple's Dev man page for sed but with a lack of examples, I couldn't piece it together.
推荐答案
Apple的手册页上说可以使用-e或-f选项指定多个命令.所以我要说>
Apple's man page says Multiple commands may be specified by using the -e or -f options. So I'd say
find . -type f -exec sed -i '' -e s/Red/$color1/g -e s/Blue/$color2/g {} \;
这当然可以在Linux和其他Unices中使用.
This certainly works in Linux and other Unices.
这篇关于使用一个sed命令进行多次替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!