0

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?

1
  • This might help: echo "<hello> <world>" | sed -E 's/(\W) (\W)/\1\n\2/g' Commented Jun 17, 2018 at 20:25

2 Answers 2

2

This one works too:

echo "<hello> <world>" | sed 's/>./>\n/g'
Sign up to request clarification or add additional context in comments.

Comments

1

Instead use:

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

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

1 Comment

If not you can also do this by using zero-length assertions, but this depends on the version of sed you are using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.