使用一个sed命令进行多次替换 [英] Multiple replacements with one sed command

查看:1165
本文介绍了使用一个sed命令进行多次替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用Mac OSX中的单个sed语句进行多次查找/替换.我可以在Ubuntu中执行此操作,但是由于OSXBSD性质,因此必须对命令进行一些更改.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆