gpt4 book ai didi

macos - 使用一个 sed 命令进行多次替换

转载 作者:行者123 更新时间:2023-12-03 10:56:46 29 4
gpt4 key购买 nike

我想知道如何使用单个 sed 进行多次查找/替换声明在 Mac OSX .我可以在 Ubuntu 中做到这一点但因为BSD OSX的性质,命令必须稍作改动。

因此,给定一个带有字符串的文件:

"Red Blue Red Blue Black Blue Red Blue Red"

我想运行导致输出的 sed 语句:
"Green Yellow Green Yellow Black Yellow Green Yellow Green"

我的两个 sed 语句带有合格的 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 {} \;

我尝试了几种分号和斜线的组合,并查看了 Apple's Dev man page for sed但由于缺乏例子,我无法拼凑起来。

最佳答案

Apple 的手册页说可以使用 -e 或 -f 选项指定多个命令。所以我会说

find . -type f -exec sed -i '' -e s/Red/$color1/g -e s/Blue/$color2/g {} \;

这当然适用于 Linux 和其他 Unices。

关于macos - 使用一个 sed 命令进行多次替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19590980/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com