Hello maheshkumar, and welcome to the Monastery!

i am getting 80800

Yes, you’re getting the wrong answer there, but before that you are also getting 136 warnings of the form:

Use of uninitialized value $destination_IP in string ne at...

which should act as a red flag that your logic is wrong somewhere. Never ignore warnings! They are there to help you.

Now, the problem with your script is that it’s finding lines like this:

ns1.sina.com.cn. 80800 IN A 202.106.184.166

which come after the list of IP addresses you are interested in. So, you need some way of turning off the search when the list has ended. Here is one possible algorithm:

clear in_list flag FOR each line in the data file IF line matches first line of a list THEN extract IP save IP as source set in_list flag ELSE IF in_list flag set THEN SWITCH line CASE line matches second list line ignore it CASE line matches list-address save addresses, indexed to current source CASE default (i.e. no longer in list) clear in_list flag ENDSWITCH ENDIF ENDELSE ENDFOR

Here is an implementation in Perl:

#! perl use strict; use warnings; my $line1_re = qr! ^ Traceroute: \s+ .* \s+ (\S+) $ !x; my $line2_re = qr! ^ traceroute !x; my $destn_re = qr! ^ # start of line \s* # optional whitesp +ace \d+ # sequence number: + nnn \s+ # whitespace (?: # EITHER (?: \d{1,3} \. ){3} \d{1,3} # IP address: nn +n.nnn.nnn.nnn | # OR \* \s+ \* \s+ \* # * * * ) !x; my (%ipToRoute, $source_IP, $in_tracing); while (my $line = <DATA>) { chomp $line; if ($line =~ /$line1_re/) { $source_IP = $1; $in_tracing = 1; } elsif ($in_tracing && $line !~ /$line2_re/) { if ($line =~ /$destn_re/) { $line =~ s/ ^ \s+ //x; my $destination_IP = (split /\s+/, $line)[1]; push @{ $ipToRoute{$source_IP} }, $destination_IP unless $destination_IP eq '*'; } else { $in_tracing = 0; } } } foreach (keys %ipToRoute) { my $final_destination = @{ $ipToRoute{$_} }[-1]; printf "Destination to %s but reached %s\n", $_, $final_destination unless $_ eq $final_destination; } __DATA__ Traceroute: 22 1291136399 1291136393 1291136393 LocalDNS home.china.co +m.cn 221.204.248.107 traceroute to 221.204.248.107 (221.204.248.107), 30 hops max, 60 byte +packets 1 134.2.173.254 0.170 ms 0.191 ms 0.185 ms etc.

With the data you supplied, this outputs:

Destination to 92.123.72.112 but reached 92.123.72.0 Destination to 78.108.81.20 but reached 217.106.1.146 Destination to 208.78.244.38 but reached 208.78.244.0

as required. Note: For improved readability, I have extracted the regular expressions using the qr operator, and annotated the third one because of its complexity.

HTH,

Athanasius <°(((><contra mundum


In reply to Re^3: Printing first and last line by Athanasius
in thread Printing first and last line by maheshkumar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.