如何使用sed替换字符串中的多个字符? [英] How to use sed to replace multiple chars in a string?

查看:51
本文介绍了如何使用sed替换字符串中的多个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用sed替换字符串的一些字符.

I want to replace some chars of a string with sed.

我尝试了以下两种方法,但是我需要知道是否存在更优雅的形式来获得相同的结果,而无需使用管道或 -e 选项:

I tried the following two approaches, but I need to know if there is a more elegant form to get the same result, without using the pipes or the -e option:

sed 's#a#A#g' test.txt | sed 's#l#23#g' > test2.txt
sed -e 's#a#A#g' -e 's#l#23#g' test.txt > test2.txt

推荐答案

您可以在单个参数中使用; 分隔命令,而不是使用多个 -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.

这篇关于如何使用sed替换字符串中的多个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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