Shubham Jaiswal
Shubham Jaiswal

Reputation: 11

Extract Part of String using cut/awk or anything

[DOSLB] : [dmob7h-002.on.bell.ca] : [61421820100992016102414274381420414330] : [[ACTIVE] ExecuteThread: '15' for queue: 'weblogic.kernel.Default (self-tuning)'] : [ca.bell.tv.doslb.infrastructure.logging.LoggingAspect] : [Ending execution of the class: ca.bell.tv.doslb.application.webservice.impl.RetrieveLocalSubscriberDelegateImpl] : [Method: getRetrieveLocalSubscriber[Call ended at: 2016-10-24 14:27:44.150] : [lasted 0 sec, 305 ms] : [WITHINLIMITS]]

I wanted to extract getRetrieveLocalSubscriber from the above string. But I can not be specific with its position and also the string since it is a service name so it will change by time in the log and the position may change but it will be in the same format, [Method: getRetrieveLocalSubscriber[Call ended at: 2016-10-24 14:27:44.150] this portion will always be the same.

and I also wanted to extract the lasted 0 sec part but the problem is the seconds will always change.

I want the output like getRetrieveLocalSubscriber in one variable and lasted 0 sec in another variable

I have tried awk command cat out_log.txt | awk -F '[:]' '{print $11}' which is giving output is getRetrieveLocalSubscriber[Call ended at

Upvotes: 0

Views: 160

Answers (2)

Mustafa DOGRU
Mustafa DOGRU

Reputation: 4112

you can try something like this;

grep -o -P '(?=\[Method: getRetrieveLocalSubscriber).*(?<=ms])' yourFile

or

 grep -o -P '(?=\[Call).*(?<=ms])' yourFile

Eg;

user@host$ grep -o -P '(?=\[Method: getRetrieveLocalSubscriber).*(?<=ms])' test
[Method: getRetrieveLocalSubscriber[Call ended at: 2016-10-24 14:27:44.150] : [lasted 0 sec, 305 ms]

user@host$ grep -o -P '(?=\[Call).*(?<=ms])' test
[Call ended at: 2016-10-24 14:27:44.150] : [lasted 0 sec, 305 ms]

Upvotes: 2

Alexander Zakharenko
Alexander Zakharenko

Reputation: 71

Try something like this:

echo "[DOSLB] : [dmob7h-002.on.bell.ca] : [61421820100992016102414274381420414330] : [[ACTIVE] ExecuteThread: '15' for queue: 'weblogic.kernel.Default (self-tuning)'] : [ca.bell.tv.doslb.infrastructure.logging.LoggingAspect] : [Ending execution of the class: ca.bell.tv.doslb.application.webservice.impl.RetrieveLocalSubscriberDelegateImpl] : [Method: getRetrieveLocalSubscriber[Call ended at: 2016-10-24 14:27:44.150] : [lasted 0 sec, 305 ms] : [WITHINLIMITS]]" | perl -ne '/Method: getRetrieveLocalSubscriber\[Call ended at: (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\.(\d+?)\] : \[lasted (\d+?) sec, (\d+?) ms\]/ && print "Call end: $1-$2-$3 $4:$5:$6.$7, lasted for $8s $9ms";'
Call end: 2016-10-24 14:27:44.150, lasted for 0s 305ms

Or, if such strings are in a file:

cat test.log | perl -ne '/Method: getRetrieveLocalSubscriber\[Call ended at: (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\.(\d+?)\] : \[lasted (\d+?) sec, (\d+?) ms\]/ && print "Call end: $1-$2-$3 $4:$5:$6.$7, lasted for $8s $9ms";'

This regular expression also accepts different "call end" time. You can replace

(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\.(\d+?)

part in this regexp with 2016-10-24 14:27:44.150 and $8 and $9 with $1 and $2 respectively to match only strings with

Call ended at: 2016-10-24 14:27:44.150

substrings.

Upvotes: 2

Related Questions