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

编程入门 行业动态 更新时间:2024-10-28 14:29:32
本文介绍了如何使用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替换字符串中的多个字符?

本文发布于:2023-11-02 18:11:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553061.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   字符串   如何使用   字符   sed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!