简体   繁体   English

正则表达式:使用sed替换两个字符串之间的字符(不包括字符串本身)

[英]Regex: Replace character between two strings (excluding the strings themselves) using sed

I have a minor problem. 我有一个小问题。 I want to replace a character/single string between two strings but would like to leave the strings themselves "unharmed" by using sed . 我想在两个字符串之间替换一个字符/单个字符串,但想通过使用sed使字符串本身“不受损害”。

The input is: 输入为:

<hello> <world>

My desired output: 我想要的输出:

<hello>
<world>

My first attempt: 我的第一次尝试:

echo "<hello> <world>" | sed 's/>.</\n/g'

The output of it: 它的输出:

<hello
world>

As you can see, the ">" from "" and the "<" from "" have been removed by using my line above. 如您所见,使用上面的行删除了“”中的“>”和“”中的“ <”。

How do I prevent it from doing so? 我如何防止它这样做?

这个也可以:

echo "<hello> <world>" | sed 's/>./>\n/g'

Instead use: 而是使用:

echo "<hello> <world>" | sed 's/>.</>\n</g'

This is possible all the characters you are replacing are static. 这可能是您要替换的所有字符都是静态的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM