There are two solutions that come to mind for you.
  1. You could use a persistent flag to create a state machine. All this means is you set a variable to true every time you see a line with "SQL". When you read in a line and the flag is true, you flip it and output that line. Something like:

    use strict; use warnings; my $flag = 0; while (my $line = <DATA>) { if ($flag) { print $line; $flag = 0; } if($line =~ /SQL/) { chomp(my $date = `date`); chomp (my $errcode = $line); print "$date - $errcode\n"; $flag = 1; }#end of if }#end of while __DATA__ Sun Sep 19 10:34:29 2010 Online Backup for GMT failed. SQL2033N An error occurred while accessing TSM during the processing o +f a database utility. Reason code: 1.
  2. You could change your default record separator ($/), and use multi-line regular expressions (Modifiers) with capturing (Extracting matches) to extract the text you want. This is usually closer to how I end up doing this sort of thing, but is dramatically more complex.

As a side note, please wrap input and expected output in code tags as well as your code. It makes downloading it easier, and more importantly, it helps us make sure our regular expressions actually match what you mean, not what's written. See Writeup Formatting Tips.


In reply to Re: Regex to end of a line from previous line by kennethk
in thread Regex to end of a line from previous line by bp4a

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.