in reply to Regex to end of a line from previous line

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.

Replies are listed 'Best First'.
Re^2: Regex to end of a line from previous line
by bp4a (Initiate) on Sep 23, 2010 at 16:17 UTC

    Thanks for the help and tips on posting here! I am just going to use the $rc (the actual error) that is used to write to the file that I was trying to read from and avoid the whole mess of multiline regex and write out the error message and time! Sorry for wasting your time on this, my fault for not seeing this variable.

    Thanks -- bp4a