Skip to content
Advertisement

Get string between two regex

I have a string that is like that(comes from a curl command’s body): The server returns an xml

Package installed in 116ms.
  </log>
</data>
<status code="200">ok</status>

Sometimes this strong can become or some other error:

<status code="500">java.lang.IllegalStateException: Archive not valid.</status>

I’m wonder what would be the best way to grep the status code 200 and if it is not 200. Give an error like “Status code is 500 with error: java.lang.IllegalStateException: Archive not valid

So far I have some improvement:

sed -n “s|<status code=”200″>(.*)|1|p” test.log

But how do I get the status code too with sed

Advertisement

Answer

To get attribute code of node status, just:

xmllint --xpath 'string(//status/@code)' -
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement