Hello again, maheshkumar,
If I understand correctly, the format of your input data has now changed, with blank lines inserted and a line reading “Trace complete.” following each list of traceroutes. And you have tried to adapt the code I gave by changing 2 of the regular expressions.
Well, your change to $line2_re is fine, but $destn_re now has (/*/) which only matches a forward slash followed immediately by an asterisk followed immediately by another forward slash — which doesn’t match your data. I think you want something like this (untested):
# Example lines:
# 1 1 ms * 1 ms 192.168.1.1
# 2 14 ms 8 ms 9 ms 73.220.38.1
my $destn_re = qr! ^ # start of line
\s* \d+ \s+ ms # 14 ms
(?: # EITHER
(?: \d+ ms ) # 8 ms
| # OR
\* # *
)
\s+ \d+ \s+ ms # 9 ms
\s+ (?: \d{1,3} \. ){3} \d{1,3} # IP address: 192.
+168.1.1
$ # end of line
!x;
You should study perlretut and then perlre to get a solid understanding of regular expressions.
However, the real problem is that the algorithm needs to be changed to match the new input format. Hint: add a CASE for blank lines, and change CASE default to match lines beginning Trace complete. (But there might be other approaches which would work just as well.) First get the algorithm clear: work it out on paper, going through the input data, and verify that the steps in the new algorithm lead to the desired result. Once you have the algorithm worked out, you will find adapting the Perl code is quite straightforward.
Keep at it, you’re making progress!
Update 1: Fixed and annotated regex in response to OP’s question, below.
Update 2: ++hbm for the solution below using Perl’s “flip-flop” operator.
Athanasius <°(((>< contra mundum
|