Two-Tu
Two-Tu

Reputation: 137

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.

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?

Upvotes: 0

Views: 59

Answers (2)

suren
suren

Reputation: 8786

This one works too:

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

Upvotes: 2

asaeles
asaeles

Reputation: 66

Instead use:

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

This is possible all the characters you are replacing are static.

Upvotes: 1

Related Questions