simon
simon

Reputation: 12902

Sed & Regex: match between two characters only sometimes successful

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)

?

Upvotes: 2

Views: 205

Answers (1)

anubhava
anubhava

Reputation: 785128

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}'

Upvotes: 6

Related Questions