Sed & Regex: match between two characters only sometimes successful

205 Views Asked by At

When I execute the command

echo '<test>' | sed -e 's/<\(.*\)>/\1/g'

I get the string

test

returned. When I execute the command

echo 'Jun 10 13:23:16 server /opt/app/introdedaemon/sbin/mq2init[14150]: Debug 1433935396.995245: (XMLRPCConnector.cpp)(MQMessage)(98)[domt initrode.om]: encoded string: <aGVyZSBpcyBub3RoaW5n c3RpbGwgbm90aGluZw== > (size 908)' | sed -e 's/<\(.*\)>/\1/g'

I would expect to get the string

aGVyZSBpcyBub3RoaW5n c3RpbGwgbm90aGluZw== 

returned, but instead I get the string

Jun 10 13:23:16 server /opt/app/introdedaemon/sbin/mq2init[14150]: Debug 1433935396.995245: (XMLRPCConnector.cpp)(MQMessage)(98)[domt initrode.om]: encoded string: aGVyZSBpcyBub3RoaW5n c3RpbGwgbm90aGluZw==  (size 908)

back. Why does this happen and with which regex can I return the part between the "<" and ">" for the string

Jun 10 13:23:16 server /opt/app/introdedaemon/sbin/mq2init[14150]: Debug 1433935396.995245: (XMLRPCConnector.cpp)(MQMessage)(98)[domt initrode.om]: encoded string: <aGVyZSBpcyBub3RoaW5n c3RpbGwgbm90aGluZw== > (size 908)

?

1

There are 1 best solutions below

1
On BEST ANSWER

You need to use:

sed 's/.*<\(.*\)>.*/\1/'

as there is a text before < and after > also.

This will give output as:

aGVyZSBpcyBub3RoaW5n c3RpbGwgbm90aGluZw==

You can get same output using this much simpler awk command also:

awk -F '[<>]' '{print $2}'