且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用sed替换字符串中的多个字符?

更新时间:2023-02-12 19:11:48

您可以在单个参数中使用; 分隔命令,而不是使用多个 -e 选项./p>

Instead of multiple -e options, you can separate commands with ; in a single argument.

sed 's/a/A/g; s/1/23/g' test.txt > test2.txt

如果您正在寻找一种在单个命令中进行多次替换的方法,我认为没有办法.如果它们都是单字符替换,则可以使用 y/abc/123 这样的命令,该命令会将 a 替换为 1 b 2 ,以及 c 3 .但是,没有多字符版本.

If you're looking for a way to do multiple substitutions in a single command, I don't think there's a way. If they were all single-character replacements you could use a command like y/abc/123, which would replace a with 1, b with 2, and c with 3. But there's no multi-character version of this.