user3834663
user3834663

Reputation: 543

regex to print only matched string not the whole line using awk

I'm trying to match only the ID "549d40r0-1e1b-01v8-0d72-0a8f2c680100" from the below data, I have the pattern match using the regex, however need to match only the ID not the whole line has the pattern match. Can you please help me using AWK.

Here is the regex: ([a-z0-9]-[a-z0-9]-[a-z0-9]-[a-z0-9]-[a-z0-9]*):

[SyncThread:server:_88285] ERROR SyncThread.logDeliveryFailure(1535) - 
Delivery for server threw runtime exception. Failed to send data: 549d40r0-
1e1b-01v8-0d72-0a8f2c680100: java.net.ConnectException: Connection refused: 
connect -- Connection refused:

Thanks and regards,

Upvotes: 2

Views: 2012

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133428

Solution 1st: In case your id(string which you posted in your post) is on a single line and you want to look only for a specific string then following may help you on same.

awk -v var="549d40r0-1e1b-01v8-0d72-0a8f2c680100" '{if(match($0,var)){print substr($0,RSTART,RLENTH)}}'  Input_file

OR

awk -v var="549d40r0-1e1b-01v8-0d72-0a8f2c680100" 'match($0,var){print substr($0,RSTART,RLENTH)}'   Input_file

Solution 2nd: In case your id(string which you posted in your post) is on a single line and you want to match it with a regex(as per only shown examples) then following may help you on same.

awk '{if(match($0,/[0-9a-z]+\-[0-9a-z]+\-[0-9a-z]+\-[0-9a-z]+\-[0-9a-z]+:/)){print substr($0,RSTART,RLENTH)}}'   Input_file

OR

awk 'match($0,/[0-9a-z]+\-[0-9a-z]+\-[0-9a-z]+\-[0-9a-z]+\-[0-9a-z]+:/){print substr($0,RSTART,RLENGTH)}'  Input_file

Upvotes: 2

Related Questions