Alex Alex - 6 months ago 9
Linux Question

How to remove a space between matching words?

I've read a lot of questions about how to replace spaces from a file but I have the following problem:

I have a file like so:

<foo>"crazy foo"</foo> <bar>dull-bar</bar>


and I'm trying to remove spaces between
> <
and only those ones so the file would be like:

`<foo>"crazy foo"</foo><bar>dull-bar</bar>`


So far I've tried to remove then by using
sed
and
tr
. Sed is not working by any chance and using
tr '> <' '><'
outputs:

<foo>"crazy foo"</foo><<bar>dull-bar</bar>

Answer
sed -i -e "s/> *</></g" YourFile

-i means YourFile is modified. Remove this option to test your command and display the result in shell output.

* matches n spaces.

The g at the end of sed expression means "Replace all the occurrences".