0

I'm trying to parse log file lines for IP addresses with grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' and would like to extract only last IP address in every given line.

Example of two lines to parse:

Via: SIP/2.0/UDP 78.41.207.101:5237;branch=z9hG4bK-577783956;rport=5237
Via: SIP/2.0/UDP 127.0.0.1:5079;branch=z9hG4bK-1014230957;rport=5079;received=194.126.22.146

I'm getting:

78.41.207.101
127.0.0.1
194.126.22.146

What I would like to get:

78.41.207.101
194.126.22.146

I would appreciate your help.

1

You can try this sed

sed -E 's/.*[^0-9](([0-9]{1,3}\.){3}[0-9]{1,3}).*/\1/' infile

Explain :

-E to use extanded regular expression.
Without -E you must write the command this way

sed 's/.*[^0-9]\(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*/\1/' infile

(([0-9]{1,3}.){3}[0-9]{1,3}) is the same RE as yours.

<-------> !  
    1     3 time

'[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'

<---------><---------><--------->

   1       2       3

The first part .*[^0-9] is greedy.
It match all it can before the last regex in the line.
The last .* match the end of the line.

  • We prefer answers that contain some explanation, and are not just a command. – G-Man Apr 26 '18 at 21:36
0

I like using awk in such cases:

$ awk '{print $NF}' FPAT="[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}" file1
78.41.207.101
194.126.22.146

FPAT is used to define what a field consists of (identical to your grep -o pattern)
$NF is the last field of each line

0

Most regexp machinery use greedy patterns, i.e., they match as much as they can. So something like ^.*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ will match up to and including the last IPv4 address. Capture that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.