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


In reply to Re^5: 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.