Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Printing first and last line

by hbm (Hermit)
on Aug 14, 2012 at 00:51 UTC ( #987244=note: print w/ replies, xml ) Need Help??


in reply to Printing first and last line

use strict; use warnings; $/='Traceroute:'; while(<DATA>){ print "wanted $1, arrived at $2\n" if /traceroute to ((?:\d+\.){3}\d+).*\D((?:\d+\.){3}\d+)/s && $1 + ne $2; } __DATA__ Traceroute: 10 1291134800 1291134792 1291134792 GoogleDNS 0.media.coll +egehumor.com 92.123.72.112 traceroute to 92.123.72.112 (92.123.72.112), 30 hops max, 60 byte pack +ets 1 * * * 2 134.2.205.30 0.765 ms 1.125 ms 1.428 ms 3 134.2.250.254 0.612 ms 0.701 ms 0.695 ms 4 129.143.135.33 0.515 ms 0.516 ms 0.510 ms 5 129.143.1.149 1.014 ms 1.012 ms 1.010 ms 6 129.143.1.166 1.036 ms 0.821 ms 0.808 ms 7 129.143.1.130 274.286 ms 274.265 ms * 8 80.81.192.28 7.246 ms 6.913 ms 6.896 ms 9 92.123.72.0 7.319 ms 7.311 ms 7.297 ms Traceroute: 12 1291134800 1291134792 1291134792 LocalDNS 0.media.colle +gehumor.com 212.201.100.184 traceroute to 212.201.100.184 (212.201.100.184), 30 hops max, 60 byte +packets 1 * * * 2 134.2.205.30 0.774 ms 1.121 ms 1.426 ms 3 134.2.250.254 0.635 ms 0.716 ms 0.709 ms 4 129.143.135.33 0.584 ms 0.583 ms 0.579 ms 5 129.143.1.149 0.951 ms 0.948 ms 0.942 ms 6 188.1.233.229 1.042 ms 0.969 ms 0.962 ms 7 188.1.145.77 4.665 ms 4.804 ms 4.826 ms 8 188.1.146.50 4.815 ms 4.890 ms 4.841 ms 9 188.1.145.73 7.071 ms 7.202 ms 7.241 ms 10 188.1.145.69 9.976 ms 10.001 ms 9.441 ms 11 212.201.100.184 9.289 ms 9.259 ms 9.276 ms

Prints:

wanted 92.123.72.112, arrived at 92.123.72.0


Comment on Re: Printing first and last line
Select or Download Code
Re^2: Printing first and last line
by maheshkumar (Sexton) on Aug 14, 2012 at 01:19 UTC

    I have written a code which is as follows

    use strict; use warnings; #!/usr/bin/perl -w use Regexp::Common qw/net/; open my $in, '<', 'Sample_01.txt' or die $!; open FILE, '>', 'filename.txt' or die $!; my %ipToRoute; my $source_IP; while (my $line = <$in>) { if (my ($ip) = $line =~ m/^Traceroute: .* (\S+)/) { $source_IP = $ip; } else { $line =~ s/^ +//; my $destination_IP = (split / +/, $line)[1]; push @{$ipToRoute{$source_IP}}, $destination_IP if $destination_IP + ne '*'; } } foreach my $source_IP (keys %ipToRoute) { my $final_destination = @{$ipToRoute{$source_IP}}[-1]; if ($source_IP ne $final_destination) { print FILE "Destination to $source_IP but reached $final_desti +nation\n"; } }

    But for 221.204.248.107 i am getting 80800

    My data can change as I have large files with some other useless stuff as well. My data is as follows

      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

        Thank you so much for the head start, I tried to modify the code according to the following format

        Traceroute: 20 1291140121 1291140102 1291140102 LocalDNS 4.bp.blogspot +.com 74.125.127.102 Tracing route to 74.125.127.102 over a maximum of 20 hops 1 1 ms * 1 ms 192.168.1.1 2 14 ms 8 ms 9 ms 73.220.38.1 3 9 ms 9 ms 11 ms 68.87.207.65 4 21 ms 13 ms 13 ms 68.85.240.101 5 11 ms 17 ms 12 ms 68.85.240.94 6 15 ms 12 ms 13 ms 68.86.93.109 7 13 ms 14 ms 15 ms 68.86.85.205 8 17 ms 14 ms 13 ms 68.86.85.42 9 14 ms 13 ms 12 ms 75.149.231.90 10 16 ms 15 ms 18 ms 209.85.249.32 11 16 ms 13 ms 14 ms 66.249.94.195 12 23 ms 20 ms 21 ms 216.239.46.200 13 24 ms 23 ms 20 ms 64.233.174.103 14 72 ms 58 ms 22 ms 216.239.46.18 15 23 ms 19 ms 24 ms 74.125.127.102 Trace complete. Query: 11 1291140122 GoogleDNS 52.mgl.skyrock.net 1 True 0.28404291513 +2 0.218060020179 id 43103 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION 52.mgl.skyrock.net. IN A ;ANSWER 52.mgl.skyrock.net. 562 IN CNAME mgl50.gslb.skyrock.net. mgl50.gslb.skyrock.net. 10 IN A 91.203.186.8 ;AUTHORITY ;ADDITIONAL Query: 11 1291140122 GoogleDNS 52.mgl.skyrock.net 1 True 0.09980143832 +56 0.203481721767 id 37287 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION 52.mgl.skyrock.net. IN A ;ANSWER 52.mgl.skyrock.net. 562 IN CNAME mgl50.gslb.skyrock.net. mgl50.gslb.skyrock.net. 10 IN A 91.203.186.8 ;AUTHORITY ;ADDITIONAL Query: 13 1291140122 OpenDNS 6.media.collegehumor.com 1 True 0.2016434 +86473 0.0180179321429 id 21546 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION 6.media.collegehumor.com. IN A ;ANSWER 6.media.collegehumor.com. 600 IN CNAME 6.media.collegehumor.com.edgesu +ite.net. 6.media.collegehumor.com.edgesuite.net. 21600 IN CNAME a1835.g.akamai. +net. a1835.g.akamai.net. 1 IN A 209.107.205.50 a1835.g.akamai.net. 1 IN A 209.107.205.51 ;AUTHORITY ;ADDITIONAL Query: 13 1291140122 OpenDNS 6.media.collegehumor.com 1 True 0.0797040 +345855 0.0161269034938 id 29064 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION 6.media.collegehumor.com. IN A ;ANSWER 6.media.collegehumor.com. 600 IN CNAME 6.media.collegehumor.com.edgesu +ite.net. 6.media.collegehumor.com.edgesuite.net. 21600 IN CNAME a1835.g.akamai. +net. a1835.g.akamai.net. 1 IN A 209.107.205.50 a1835.g.akamai.net. 1 IN A 209.107.205.51 ;AUTHORITY ;ADDITIONAL Traceroute: 18 1291140122 1291140104 1291140104 GoogleDNS 4.media.coll +egehumor.com 92.122.217.179 Tracing route to 92.122.217.179 over a maximum of 20 hops 1 1 ms * 1 ms 192.168.1.1 2 11 ms 10 ms 8 ms 73.220.38.1 3 8 ms 7 ms 11 ms 68.87.207.65 4 24 ms 10 ms 8 ms 68.85.240.101 5 8 ms 10 ms 9 ms 68.85.240.94 6 21 ms 14 ms 14 ms 68.86.93.109 7 27 ms 27 ms 26 ms 68.86.85.162 8 29 ms 30 ms 32 ms 68.86.85.46 9 34 ms 32 ms 32 ms 68.86.87.6 10 29 ms 31 ms 30 ms 80.156.163.153 11 191 ms 243 ms 257 ms 62.154.5.25 12 186 ms 189 ms 204 ms 80.146.198.54 13 227 ms 193 ms 214 ms 92.122.217.179 Trace complete.

        I think so I have written the right Regular Expressions but am not getting the output. Here is the code i modified from yours. Only the part where you are defining the lines format.

        my $line1_re = qr! ^ Traceroute: \s+ .* \s+ (\S+) $ !x; my $line2_re = qr! ^ Tracing route !x; my $destn_re = qr! ^ \s*\d+(\s+\d+\sms \s+){3} (?:(?: \d{1,3} \. ){3} +\d{1,3}|(/*/) )!x;

        Just one last quick question what i am trying to do is to compare the two IPs if the first three octet are similar or not. For example Destination to 92.123.72.112 but reached 92.123.72.0 in this the first three are similar that means 92.123.72 are same so i want to put these two IPs in a hash and the key to them will be their location which i will find with another code. How is this possible?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://987244]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2014-11-26 21:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (174 votes), past polls