nomazoma49 nomazoma49 - 4 months ago 11
Bash Question

How to remove only one newline between > [greater than] and < [less than] in unix

Lets consider that there is a text in the following format:

"this\n is >\n<"


and i want to truncate the newline only between > and < which will result in:

"this\n is ><"


How to achieve this ?

I tried using the following:

echo "this\n is >\n<" | sed -e 's/>\n<//g'


and

echo "this\n is >\n<" | sed -e 's/>\\n</></g'


But none of them work out. Any suggestions from genius minds ?

Answer

This might work for you (GNU sed):

sed ':a;N;s/>\n</></;ta;P;D' file

Read two lines into the pattern space and if the newline is between > and < remove it. Then print and delete the first line and repeat.