John R
John R

Reputation: 3026

Perl/Regex to parse a string

I am new to Perl and a little stumped on parsing the lines for a traceroute. This is typical of the output that I am challenged with.

 3  someURL.net (184.106.126.128)  0.579 ms someURL.net (184.106.126.124)  0.742 ms  0.719 ms

Note how the 1st and 2nd routes use a different IP address. This will change; e.g., sometimes the hops will use all the same like this:

3  someURL.net (184.106.126.128)  0.579 ms  0.742 ms  0.719 ms

...or, each hop could have a different route, or they could all be the same. I would like to use some regex, etc. in Perl to format the output like this:

3|url~ip~time|url~ip~time|url~ip~time

I'm not sure if I should loop through the individual words somehow, or if it is easier to do it all with regex. Any solutions or hints appreciated.

Upvotes: 0

Views: 509

Answers (2)

kitwalker
kitwalker

Reputation: 982

would this regex work

((\w+?\.\w+?)\ (\(\d+?\.\d+?\.\d+?\.\d+?\))(\ \d+?\.\d+?\ ms)+\ )+
     URL                  IP                    TIME

Upvotes: 0

mjbnz
mjbnz

Reputation: 683

Would it not be easier to use a traceroute module for perl? Is there any reason you need to parse raw traceroute output? (If just as an exercise for learning regex, sure.. but?)

Net::Traceroute would be what I'd use.

Upvotes: 5

Related Questions