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

Re^4: Printing first and last line

by maheshkumar (Sexton)
on Aug 14, 2012 at 13:13 UTC ( [id://987349]=note: print w/replies, xml ) Need Help??


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

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;

Replies are listed 'Best First'.
Re^5: Printing first and last line
by hbm (Hermit) on Aug 14, 2012 at 14:19 UTC

    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
      I am not sure but it is not working - any ideas?
      for example 192.168.2.1 reached to 192.168.2.1 so i dont need that, it is giving me that result as well

        I left a little work for you. Surely you can compare $want and $got, and print only if they differ?

Re^5: Printing first and last line
by Athanasius (Archbishop) on Aug 14, 2012 at 14:13 UTC

    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

      There will be 3 (ms) right as ms appears thrice so are you missing one in the one that you have put?

        Can you advice something to read on the errors i keep on getting, like uninitialised or sometimes global variable and all

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-23 19:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found