user1155413
user1155413

Reputation: 169

extract unique value from a log4j log file

Im having trouble extracting only a matching string: OPER^ from a log4j file. I can get this value from two different sources inside my log file:

2012-01-26 03:06:45,428 INFO  [NP_OSS] OSSBSSGWIMPL6000|**OPR20120126120537008893**|GenServiceDeactivationResponse :: processRequestGenServiceDeactivationResponse() ::

or:

2012-01-26 03:06:45,411 INFO  [NP_OSS] MESSAGE_DATA = <?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:ServiceDeactivationResponse xmlns:ns2="urn:ngn:foo"><MessageHeader><MessageTimeStamp>20120126031123</MessageTimeStamp>**<OperatorTrxID>OPR20120126120537008893</OperatorTrxID>**</MessageHeader></ns2:ServiceDeactivationResponse>

I need to extract only the value OPR* I'm guessing its much easier to extract it from the first one since it doesn't involve parsing xml.

Thanks a lot in advance for your help!

Upvotes: 0

Views: 908

Answers (3)

Kent
Kent

Reputation: 195289

maybe I didn't understand OP's question well, why a simple grep command cannot do the job?

like

grep -Po 'OPR\d+'

output for both lines are same:

OPR20120126120537008893

Upvotes: 5

jaypal singh
jaypal singh

Reputation: 77185

awk: Setting up Field Separators

awk -v FS="[<>]" '{print $13}' logfile

perl: Using Positive look ahead and look behind

perl -pne 's/.*(?<=\<OperatorTrxID\>)([A-Z0-9]+)(?=\<\/OperatorTrxID\>).*/$1/' logfile

Test:

[jaypal:~/Temp] cat logfile
2012-01-26 03:06:45,411 INFO  [NP_OSS] MESSAGE_DATA = <?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:ServiceDeactivationResponse xmlns:ns2="urn:ngn:foo"><MessageHeader><MessageTimeStamp>20120126031123</MessageTimeStamp><OperatorTrxID>OPR20120126120537008893</OperatorTrxID></MessageHeader></ns2:ServiceDeactivationResponse>

[jaypal:~/Temp] awk -v FS="[<>]" '{print $13}' logfile
OPR20120126120537008893

[jaypal:~/Temp] perl -pne 's/.*(?<=\<OperatorTrxID\>)([A-Z0-9]+)(?=\<\/OperatorTrxID\>).*/$1/' logfile
OPR20120126120537008893

Upvotes: 2

user647772
user647772

Reputation:

$ echo $line | grep OPR | sed -e "s/^.*OPR\([0-9]*\).*$/\1/" 

Edit:

After reading your comment:

$ echo $line | grep OPR | sed -e "s/^.*\(OPR[0-9]*\).*$/\1/" | head -1

Upvotes: 2

Related Questions