Home > database >  How to remove unwanted newline characters and tab spaces before XML closing tag
How to remove unwanted newline characters and tab spaces before XML closing tag

Time:03-28

I need to remove the new line and empty tab space/extra spaces before URL closing tag(). I am looking for a regular expression for doing it as I have to make changes in multiple directories.

<RouteRule name="default">
  <URL>http://api.mycompany.com/myAPI
  </URL> 
</RouteRule>

Any leads would be appreciated.

CodePudding user response:

You can try to find everything except tags and spaces

This /(<\w >)\s*([^<>\s] )\s*(<\/\w >)/gm should capture the opening and closing tag and the content.

https://regex101.com/r/CCx00C/1

CodePudding user response:

Maybe you need to do it this way:

Find:<URL>([^\r\n]*)\r\n([^<]*)

replace:<URL>\1

CodePudding user response:

To match a string across multiple lines you can first replace newlines with e.g. carriage return, then apply the regular expression and finally replace carriage return with newline:

tr '\n' '\r' | sed -E 's|[ \t\r] </URL>|</URL>|' | tr '\r' '\n'

For example:

$ cat input.xml 
<RouteRule name="default">
  <URL>http://api.mycompany.com/myAPI
  </URL> 
</RouteRule>

$ cat input.xml | tr '\n' '\r' | sed -E 's|[ \t\r] </URL>|</URL>|' | tr '\r' '\n'
<RouteRule name="default">
  <URL>http://api.mycompany.com/myAPI</URL> 
</RouteRule>
  • Related