in reply to Re: Printing first and last line
in thread Printing first and last line

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

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 2 134.2.205.30 0.788 ms 1.174 ms 1.437 ms 3 134.2.250.254 0.545 ms 0.670 ms 0.721 ms 4 129.143.135.33 0.500 ms 0.494 ms 0.486 ms 5 129.143.1.149 0.904 ms 0.887 ms 0.879 ms 6 129.143.1.166 0.968 ms 0.979 ms 0.941 ms 7 129.143.1.130 282.007 ms 281.678 ms * 8 213.248.88.25 44.918 ms 44.917 ms 44.817 ms 9 80.91.248.97 6.871 ms 80.91.248.101 6.850 ms 80.91.249.100 6.90 +7 ms 10 80.91.246.64 99.000 ms 80.91.246.58 98.446 ms 80.91.246.64 98.9 +71 ms 11 80.91.252.220 176.766 ms 80.91.248.204 177.302 ms 80.91.253.68 +175.896 ms 12 213.248.73.190 176.999 ms 175.746 ms 177.042 ms 13 219.158.30.229 364.847 ms 366.518 ms 366.292 ms 14 219.158.11.149 366.285 ms 365.532 ms 363.345 ms 15 219.158.20.70 406.917 ms 405.713 ms 405.474 ms 16 218.26.125.46 414.111 ms 412.774 ms 412.972 ms 17 221.204.208.54 421.544 ms 425.869 ms 422.572 ms 18 221.204.236.230 417.560 ms 415.790 ms 415.781 ms 19 221.204.252.252 423.594 ms 421.487 ms 413.455 ms 20 221.204.236.150 414.844 ms 412.047 ms 413.006 ms 21 221.204.248.107 431.374 ms 429.395 ms 429.087 ms Query: 10 1291136399 OpenDNS hosting.adjug.com 1 True 0.0251162052155 +0.0313730239868 id 38174 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hosting.adjug.com. IN A ;ANSWER hosting.adjug.com. 2283 IN A 85.90.254.44 ;AUTHORITY ;ADDITIONAL Query: 10 1291136399 OpenDNS hosting.adjug.com 1 True 0.0231759548187 +0.0305850505829 id 50729 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hosting.adjug.com. IN A ;ANSWER hosting.adjug.com. 2283 IN A 85.90.254.44 ;AUTHORITY ;ADDITIONAL Query: 16 1291136399 LocalDNS hosting.adjug.com 1 True 0.0008859634399 +41 0.0309240818024 id 19761 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hosting.adjug.com. IN A ;ANSWER hosting.adjug.com. 1095 IN A 85.90.254.44 ;AUTHORITY adjug.com. 1087 IN NS dns1.lon6.telecity.net. adjug.com. 1087 IN NS dns1.lon7.telecity.net. adjug.com. 1087 IN NS dns1.lon2.telecity.net. ;ADDITIONAL dns1.lon2.telecity.net. 73569 IN A 85.90.229.164 dns1.lon6.telecity.net. 76378 IN A 212.111.32.41 dns1.lon7.telecity.net. 74574 IN A 213.52.192.66 Query: 16 1291136399 LocalDNS hosting.adjug.com 1 True 0.0008041858673 +1 0.0303239822388 id 48960 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hosting.adjug.com. IN A ;ANSWER hosting.adjug.com. 1095 IN A 85.90.254.44 ;AUTHORITY adjug.com. 1087 IN NS dns1.lon2.telecity.net. adjug.com. 1087 IN NS dns1.lon6.telecity.net. adjug.com. 1087 IN NS dns1.lon7.telecity.net. ;ADDITIONAL dns1.lon2.telecity.net. 73569 IN A 85.90.229.164 dns1.lon6.telecity.net. 76378 IN A 212.111.32.41 dns1.lon7.telecity.net. 74574 IN A 213.52.192.66 Query: 10 1291136399 GoogleDNS hosting.adjug.com 1 True 0.008981943130 +49 0.0303249359131 id 1007 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hosting.adjug.com. IN A ;ANSWER hosting.adjug.com. 857 IN A 85.90.254.44 ;AUTHORITY ;ADDITIONAL Query: 10 1291136399 GoogleDNS hosting.adjug.com 1 True 0.008209943771 +36 0.0308527946472 id 17027 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hosting.adjug.com. IN A ;ANSWER hosting.adjug.com. 856 IN A 85.90.254.44 ;AUTHORITY ;ADDITIONAL Query: 10 1291136399 OpenDNS hotlinkimage.com 1 True 0.161711931229 0. +102969884872 id 38089 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hotlinkimage.com. IN A ;ANSWER hotlinkimage.com. 14400 IN A 70.38.71.43 ;AUTHORITY ;ADDITIONAL Query: 10 1291136399 OpenDNS hotlinkimage.com 1 True 0.0270168781281 0 +.0974688529968 id 43399 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hotlinkimage.com. IN A ;ANSWER hotlinkimage.com. 14400 IN A 70.38.71.43 ;AUTHORITY ;ADDITIONAL Query: 16 1291136400 LocalDNS hotlinkimage.com 1 True 0.164645910263 0 +.0993812084198 id 2368 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hotlinkimage.com. IN A ;ANSWER hotlinkimage.com. 14400 IN A 70.38.71.43 ;AUTHORITY hotlinkimage.com. 90485 IN NS ns1.dreamhost.com. hotlinkimage.com. 90485 IN NS ns2.dreamhost.com. hotlinkimage.com. 90485 IN NS ns3.dreamhost.com. ;ADDITIONAL ns1.dreamhost.com. 5546 IN A 66.33.206.206 ns2.dreamhost.com. 5546 IN A 208.96.10.221 ns3.dreamhost.com. 8196 IN A 66.33.216.216 Query: 16 1291136400 LocalDNS hotlinkimage.com 1 True 0.00077891349792 +5 0.104137897491 id 42414 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hotlinkimage.com. IN A ;ANSWER hotlinkimage.com. 14400 IN A 70.38.71.43 ;AUTHORITY hotlinkimage.com. 90485 IN NS ns3.dreamhost.com. hotlinkimage.com. 90485 IN NS ns1.dreamhost.com. hotlinkimage.com. 90485 IN NS ns2.dreamhost.com. ;ADDITIONAL ns1.dreamhost.com. 5546 IN A 66.33.206.206 ns2.dreamhost.com. 5546 IN A 208.96.10.221 ns3.dreamhost.com. 8196 IN A 66.33.216.216 Query: 10 1291136400 GoogleDNS hotlinkimage.com 1 True 0.0100698471069 + 0.104535102844 id 56229 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hotlinkimage.com. IN A ;ANSWER hotlinkimage.com. 9651 IN A 70.38.71.43 ;AUTHORITY ;ADDITIONAL Query: 10 1291136400 GoogleDNS hotlinkimage.com 1 True 0.0088081359863 +3 0.0986399650574 id 34442 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hotlinkimage.com. IN A ;ANSWER hotlinkimage.com. 9651 IN A 70.38.71.43 ;AUTHORITY ;ADDITIONAL Query: 10 1291136400 OpenDNS hp-geoisp.virgilio.it 1 True 0.0505359172 +821 0.0298049449921 id 1989 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp-geoisp.virgilio.it. IN A ;ANSWER hp-geoisp.virgilio.it. 3600 IN A 212.48.11.161 ;AUTHORITY ;ADDITIONAL Query: 10 1291136400 OpenDNS hp-geoisp.virgilio.it 1 True 0.0269870758 +057 0.0302181243896 id 61894 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp-geoisp.virgilio.it. IN A ;ANSWER hp-geoisp.virgilio.it. 3600 IN A 212.48.11.161 ;AUTHORITY ;ADDITIONAL Query: 16 1291136400 LocalDNS hp-geoisp.virgilio.it 1 True 0.019423961 +6394 0.0299680233002 id 1812 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp-geoisp.virgilio.it. IN A ;ANSWER hp-geoisp.virgilio.it. 3600 IN A 212.48.11.161 ;AUTHORITY virgilio.it. 3548 IN NS dns1.virgilio.net. virgilio.it. 3548 IN NS dns2.virgilio.net. virgilio.it. 3548 IN NS dnsdom.tin.it. ;ADDITIONAL dns1.virgilio.net. 3548 IN A 212.48.4.10 dns2.virgilio.net. 3548 IN A 212.48.4.9 dnsdom.tin.it. 3548 IN A 194.243.154.57 Query: 16 1291136400 LocalDNS hp-geoisp.virgilio.it 1 True 0.001061916 +35132 0.029853105545 id 45094 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp-geoisp.virgilio.it. IN A ;ANSWER hp-geoisp.virgilio.it. 3600 IN A 212.48.11.161 ;AUTHORITY virgilio.it. 3548 IN NS dnsdom.tin.it. virgilio.it. 3548 IN NS dns1.virgilio.net. virgilio.it. 3548 IN NS dns2.virgilio.net. ;ADDITIONAL dns1.virgilio.net. 3548 IN A 212.48.4.10 dns2.virgilio.net. 3548 IN A 212.48.4.9 dnsdom.tin.it. 3548 IN A 194.243.154.57 Query: 10 1291136400 GoogleDNS hp-geoisp.virgilio.it 1 True 0.02439403 +53394 0.0299079418182 id 44217 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp-geoisp.virgilio.it. IN A ;ANSWER hp-geoisp.virgilio.it. 3600 IN A 212.48.11.161 ;AUTHORITY ;ADDITIONAL Query: 10 1291136400 GoogleDNS hp-geoisp.virgilio.it 1 True 0.01091909 +40857 0.0299010276794 id 54048 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp-geoisp.virgilio.it. IN A ;ANSWER hp-geoisp.virgilio.it. 3600 IN A 212.48.11.161 ;AUTHORITY ;ADDITIONAL Query: 10 1291136400 OpenDNS hp-wetter.mfserv.net 1 True 0.04259109497 +07 0.0103650093079 id 35534 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp-wetter.mfserv.net. IN A ;ANSWER hp-wetter.mfserv.net. 3600 IN A 217.72.203.149 ;AUTHORITY ;ADDITIONAL Query: 10 1291136400 OpenDNS hp-wetter.mfserv.net 1 True 0.03959798812 +87 0.0102210044861 id 18165 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp-wetter.mfserv.net. IN A ;ANSWER hp-wetter.mfserv.net. 3600 IN A 217.72.203.149 ;AUTHORITY ;ADDITIONAL Query: 14 1291136400 LocalDNS hp-wetter.mfserv.net 1 True 0.0114591121 +674 0.00957298278809 id 49422 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp-wetter.mfserv.net. IN A ;ANSWER hp-wetter.mfserv.net. 3600 IN A 217.72.203.149 ;AUTHORITY mfserv.net. 91118 IN NS nsx2.web.de. mfserv.net. 91118 IN NS nsx1.web.de. ;ADDITIONAL nsx1.web.de. 33060 IN A 217.72.199.153 nsx2.web.de. 32218 IN A 217.72.196.250 Query: 14 1291136400 LocalDNS hp-wetter.mfserv.net 1 True 0.0011727809 +906 0.0102560520172 id 44948 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp-wetter.mfserv.net. IN A ;ANSWER hp-wetter.mfserv.net. 3600 IN A 217.72.203.149 ;AUTHORITY mfserv.net. 91118 IN NS nsx1.web.de. mfserv.net. 91118 IN NS nsx2.web.de. ;ADDITIONAL nsx1.web.de. 33060 IN A 217.72.199.153 nsx2.web.de. 32218 IN A 217.72.196.250 Query: 10 1291136400 GoogleDNS hp-wetter.mfserv.net 1 True 0.013778924 +942 0.0100429058075 id 64924 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp-wetter.mfserv.net. IN A ;ANSWER hp-wetter.mfserv.net. 840 IN A 217.72.203.149 ;AUTHORITY ;ADDITIONAL Query: 10 1291136400 GoogleDNS hp-wetter.mfserv.net 1 True 0.009327888 +48877 0.0105588436127 id 60379 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp-wetter.mfserv.net. IN A ;ANSWER hp-wetter.mfserv.net. 840 IN A 217.72.203.149 ;AUTHORITY ;ADDITIONAL Query: 12 1291136401 OpenDNS hp.msn.com 1 True 0.0348389148712 0.02153 +49197388 id 43945 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp.msn.com. IN A ;ANSWER hp.msn.com. 702 IN CNAME msnimstore.vo.msecnd.net. msnimstore.vo.msecnd.net. 256 IN A 213.199.149.214 msnimstore.vo.msecnd.net. 256 IN A 213.199.149.217 ;AUTHORITY ;ADDITIONAL Query: 12 1291136401 OpenDNS hp.msn.com 1 True 0.0262739658356 0.02183 +29429626 id 40252 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp.msn.com. IN A ;ANSWER hp.msn.com. 702 IN CNAME msnimstore.vo.msecnd.net. msnimstore.vo.msecnd.net. 256 IN A 213.199.149.214 msnimstore.vo.msecnd.net. 256 IN A 213.199.149.217 ;AUTHORITY ;ADDITIONAL Query: 14 1291136401 LocalDNS hp.msn.com 1 True 0.101494073868 0.01350 +99887848 id 12328 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp.msn.com. IN A ;ANSWER hp.msn.com. 900 IN CNAME msnimstore.vo.msecnd.net. msnimstore.vo.msecnd.net. 188 IN A 94.245.68.201 msnimstore.vo.msecnd.net. 188 IN A 94.245.68.217 ;AUTHORITY msecnd.net. 297 IN NS dns12.msecnd.net. msecnd.net. 297 IN NS dns11.msecnd.net. ;ADDITIONAL Query: 14 1291136401 LocalDNS hp.msn.com 1 True 0.000933885574341 0.01 +30989551544 id 15282 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp.msn.com. IN A ;ANSWER hp.msn.com. 900 IN CNAME msnimstore.vo.msecnd.net. msnimstore.vo.msecnd.net. 188 IN A 94.245.68.217 msnimstore.vo.msecnd.net. 188 IN A 94.245.68.201 ;AUTHORITY msecnd.net. 297 IN NS dns11.msecnd.net. msecnd.net. 297 IN NS dns12.msecnd.net. ;ADDITIONAL Query: 12 1291136401 GoogleDNS hp.msn.com 1 True 0.00960683822632 0.02 +18770503998 id 41572 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp.msn.com. IN A ;ANSWER hp.msn.com. 843 IN CNAME msnimstore.vo.msecnd.net. msnimstore.vo.msecnd.net. 98 IN A 213.199.149.231 msnimstore.vo.msecnd.net. 98 IN A 213.199.149.253 ;AUTHORITY ;ADDITIONAL Query: 12 1291136401 GoogleDNS hp.msn.com 1 True 0.0090651512146 0.021 +8391418457 id 4425 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hp.msn.com. IN A ;ANSWER hp.msn.com. 843 IN CNAME msnimstore.vo.msecnd.net. msnimstore.vo.msecnd.net. 98 IN A 213.199.149.231 msnimstore.vo.msecnd.net. 98 IN A 213.199.149.253 ;AUTHORITY ;ADDITIONAL Query: 22 1291136402 OpenDNS hq.sinajs.cn 1 True 1.08146214485 0.43127 +2029877 id 33935 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hq.sinajs.cn. IN A ;ANSWER hq.sinajs.cn. 30 IN CNAME idc-hq-tj.sinajs.cn. idc-hq-tj.sinajs.cn. 30 IN A 60.28.164.212 idc-hq-tj.sinajs.cn. 30 IN A 60.28.164.213 idc-hq-tj.sinajs.cn. 30 IN A 60.28.164.214 idc-hq-tj.sinajs.cn. 30 IN A 60.28.2.66 idc-hq-tj.sinajs.cn. 30 IN A 60.28.2.67 idc-hq-tj.sinajs.cn. 30 IN A 60.28.113.208 idc-hq-tj.sinajs.cn. 30 IN A 60.28.113.209 idc-hq-tj.sinajs.cn. 30 IN A 60.28.113.210 idc-hq-tj.sinajs.cn. 30 IN A 60.28.164.49 idc-hq-tj.sinajs.cn. 30 IN A 60.28.2.68 idc-hq-tj.sinajs.cn. 30 IN A 60.28.2.64 idc-hq-tj.sinajs.cn. 30 IN A 60.28.2.65 ;AUTHORITY ;ADDITIONAL Query: 22 1291136403 OpenDNS hq.sinajs.cn 1 True 0.0229930877686 0.428 +879022598 id 28448 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hq.sinajs.cn. IN A ;ANSWER hq.sinajs.cn. 30 IN CNAME idc-hq-tj.sinajs.cn. idc-hq-tj.sinajs.cn. 30 IN A 60.28.164.212 idc-hq-tj.sinajs.cn. 30 IN A 60.28.164.213 idc-hq-tj.sinajs.cn. 30 IN A 60.28.164.214 idc-hq-tj.sinajs.cn. 30 IN A 60.28.2.66 idc-hq-tj.sinajs.cn. 30 IN A 60.28.2.67 idc-hq-tj.sinajs.cn. 30 IN A 60.28.113.208 idc-hq-tj.sinajs.cn. 30 IN A 60.28.113.209 idc-hq-tj.sinajs.cn. 30 IN A 60.28.113.210 idc-hq-tj.sinajs.cn. 30 IN A 60.28.164.49 idc-hq-tj.sinajs.cn. 30 IN A 60.28.2.68 idc-hq-tj.sinajs.cn. 30 IN A 60.28.2.64 idc-hq-tj.sinajs.cn. 30 IN A 60.28.2.65 ;AUTHORITY ;ADDITIONAL Query: 28 1291136404 LocalDNS hq.sinajs.cn 1 True 0.820191144943 0.422 +012805939 id 61094 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hq.sinajs.cn. IN A ;ANSWER hq.sinajs.cn. 30 IN CNAME idc-hq-tj.sinajs.cn. idc-hq-tj.sinajs.cn. 30 IN A 60.28.2.64 idc-hq-tj.sinajs.cn. 30 IN A 60.28.2.65 idc-hq-tj.sinajs.cn. 30 IN A 60.28.2.66 idc-hq-tj.sinajs.cn. 30 IN A 60.28.2.67 idc-hq-tj.sinajs.cn. 30 IN A 60.28.2.68 idc-hq-tj.sinajs.cn. 30 IN A 60.28.113.208 idc-hq-tj.sinajs.cn. 30 IN A 60.28.113.209 idc-hq-tj.sinajs.cn. 30 IN A 60.28.113.210 idc-hq-tj.sinajs.cn. 30 IN A 60.28.164.49 idc-hq-tj.sinajs.cn. 30 IN A 60.28.164.212 idc-hq-tj.sinajs.cn. 30 IN A 60.28.164.213 idc-hq-tj.sinajs.cn. 30 IN A 60.28.164.214 ;AUTHORITY sinajs.cn. 477 IN NS ns1.sina.com.cn. sinajs.cn. 477 IN NS ns2.sina.com.cn. sinajs.cn. 477 IN NS ns3.sina.com.cn. ;ADDITIONAL ns1.sina.com.cn. 80801 IN A 202.106.184.166 ns2.sina.com.cn. 21635 IN A 61.172.201.254 ns3.sina.com.cn. 80801 IN A 202.108.44.55 Query: 28 1291136404 LocalDNS hq.sinajs.cn 1 True 0.00102806091309 0.4 +09356832504 id 18231 opcode QUERY rcode NOERROR flags QR RD RA ;QUESTION hq.sinajs.cn. IN A ;ANSWER hq.sinajs.cn. 29 IN CNAME idc-hq-tj.sinajs.cn. idc-hq-tj.sinajs.cn. 29 IN A 60.28.164.214 idc-hq-tj.sinajs.cn. 29 IN A 60.28.2.64 idc-hq-tj.sinajs.cn. 29 IN A 60.28.2.65 idc-hq-tj.sinajs.cn. 29 IN A 60.28.2.66 idc-hq-tj.sinajs.cn. 29 IN A 60.28.2.67 idc-hq-tj.sinajs.cn. 29 IN A 60.28.2.68 idc-hq-tj.sinajs.cn. 29 IN A 60.28.113.208 idc-hq-tj.sinajs.cn. 29 IN A 60.28.113.209 idc-hq-tj.sinajs.cn. 29 IN A 60.28.113.210 idc-hq-tj.sinajs.cn. 29 IN A 60.28.164.49 idc-hq-tj.sinajs.cn. 29 IN A 60.28.164.212 idc-hq-tj.sinajs.cn. 29 IN A 60.28.164.213 ;AUTHORITY sinajs.cn. 476 IN NS ns3.sina.com.cn. sinajs.cn. 476 IN NS ns1.sina.com.cn. sinajs.cn. 476 IN NS ns2.sina.com.cn. ;ADDITIONAL ns1.sina.com.cn. 80800 IN A 202.106.184.166 ns2.sina.com.cn. 21634 IN A 61.172.201.254 ns3.sina.com.cn. 80800 IN A 202.108.44.55 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 Traceroute: 24 1291144000 1291143994 1291143994 OpenDNS www.openpaths. +com 208.78.244.38 traceroute to 208.78.244.38 (208.78.244.38), 30 hops max, 60 byte pack +ets 1 * * * 2 134.2.205.30 0.819 ms 1.156 ms 1.693 ms 3 134.2.250.254 0.621 ms 0.624 ms 0.678 ms 4 129.143.135.33 0.583 ms 0.575 ms 0.564 ms 5 129.143.1.149 0.820 ms 0.811 ms 0.801 ms 6 129.143.1.166 1.517 ms 0.986 ms 0.961 ms 7 129.143.1.130 6.880 ms 7.015 ms * 8 213.248.88.25 6.723 ms 6.711 ms 6.572 ms 9 80.91.252.173 6.797 ms 80.91.248.97 6.912 ms 80.91.252.175 6.89 +9 ms 10 80.91.246.62 100.082 ms 80.91.246.60 105.158 ms 105.201 ms 11 213.248.89.178 99.333 ms 213.248.88.86 99.730 ms 213.248.103.234 + 98.635 ms 12 4.68.17.254 99.303 ms 99.299 ms 4.68.17.62 104.978 ms 13 4.69.134.157 99.426 ms 4.69.134.149 99.610 ms 4.69.134.157 100. +356 ms 14 4.69.132.90 98.361 ms 99.334 ms 99.390 ms 15 4.69.135.254 98.560 ms 99.941 ms 100.266 ms 16 4.69.141.21 99.972 ms 99.703 ms 98.238 ms 17 4.69.135.185 187.776 ms 186.894 ms 182.739 ms 18 4.69.134.246 177.213 ms 177.345 ms 4.69.134.250 179.786 ms 19 4.68.18.20 174.221 ms 175.589 ms 4.68.18.148 174.203 ms 20 4.53.30.38 176.896 ms 176.730 ms 177.031 ms 21 208.166.59.166 177.690 ms 176.853 ms 177.565 ms 22 208.166.59.6 175.805 ms 174.682 ms 174.776 ms 23 208.78.244.0 176.786 ms 176.652 ms 177.833 ms Traceroute: 31 1291141262 1291141247 1291141247 OpenDNS www.dkd.spb.ru + 78.108.81.20 traceroute to 78.108.81.20 (78.108.81.20), 30 hops max, 60 byte packet +s 1 134.2.173.254 0.210 ms 0.190 ms * 2 134.2.205.30 0.866 ms 1.137 ms 1.457 ms 3 134.2.250.254 0.563 ms 0.642 ms 0.779 ms 4 129.143.135.33 0.494 ms 0.488 ms 0.477 ms 5 129.143.1.149 0.850 ms 0.841 ms 0.830 ms 6 129.143.1.166 0.893 ms 0.884 ms 0.870 ms 7 129.143.1.130 6.801 ms 6.787 ms * 8 213.248.88.25 6.741 ms 6.720 ms 6.569 ms 9 80.91.246.224 6.926 ms 80.91.249.102 7.090 ms 80.91.246.218 7.0 +65 ms 10 80.91.251.122 7.061 ms 80.91.251.126 7.024 ms 80.91.247.187 6.9 +49 ms 11 4.68.63.121 7.046 ms 7.036 ms 4.68.62.129 7.589 ms 12 4.68.23.78 7.689 ms 7.661 ms 7.654 ms 13 195.16.161.78 61.661 ms 61.655 ms 61.518 ms 14 217.106.0.13 62.065 ms 217.106.7.129 61.906 ms 217.106.0.13 61. +835 ms 15 195.161.47.126 65.145 ms 64.784 ms 195.161.47.122 64.793 ms 16 195.161.4.46 61.949 ms 61.932 ms 62.254 ms 17 217.106.6.161 62.623 ms 62.666 ms 217.106.0.13 62.509 ms 18 217.106.0.6 63.054 ms 62.917 ms 62.573 ms 19 217.106.1.146 62.522 ms 62.381 ms 62.369 ms 20 * * * 21 * * * 22 * * * 23 * * * 24 * * * 25 * * * 26 * * * 27 * * * 28 * * * 29 * * * 30 * * *

Replies are listed 'Best First'.
Re^3: Printing first and last line
by Athanasius (Archbishop) on Aug 14, 2012 at 07:24 UTC

    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

      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?

        I think the following little script should be enough to show you how to proceed:

        #! perl use strict; use warnings; use diagnostics; use Data::Dumper; my $destination = '92.123.72.112'; my $final_ip = '92.123.72.0'; # Get the first 3 octets of each IP my $destn_prefix = $destination =~ s/ \. \d{1,3} $ //rx; my $final_prefix = $final_ip =~ s/ \. \d{1,3} $ //rx; # Populate the hash my %hash; if ($destn_prefix eq $final_prefix) { $hash{$destn_prefix} = [ $destination, $final_ip ]; } print Dumper(\%hash), "\n"; # Access a given location my $ip = '92.123.72'; if (exists $hash{$ip}) { my @ips = @{ $hash{$ip} }; print 'IPs at location ', $ip, ' are: ', join(', ', @ips), "\n"; }

        Output:

        $VAR1 = { '92.123.72' => [ '92.123.72.112', '92.123.72.0' ] }; IPs at location 92.123.72 are: 92.123.72.112, 92.123.72.0

        Some useful references:

        Hope that helps,

        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;

        This new data format is a good candidate for the "flip-flop" operator:

        use strict; use warnings; my ($want, $got); while(<DATA>){ if (/Tracing route to/ .. /Trace complete/) { if (/Tracing route to ((?:\d+\.){3}\d+)/) { $want = $1; } elsif (/((?:\d+\.){3}\d+)/){ $got = $1; } elsif (/Trace complete/) { print "wanted $want, got $got\n"; $want = $got = ""; } } } __DATA__ 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.

        Prints:

        wanted 74.125.127.102, got 74.125.127.102 wanted 92.122.217.179, got 92.122.217.179

        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