title: Perl Program to parse through error log file, extract error message and output to new file
body: <p>I need to write a perl program where I parse through an error log and output the error messages to a new file. I am having issues with setting up the regex to do this. In the error log, an error code starts with the word "ERROR" and the end of each error message ends with a ". " (period and then a space). I want to find all the errors, count them, and also output the entire error message of each error message to a new file.</p>
  
  <p>I tried this but am having issues:</p>
  
  <pre><code>open(FH,"&lt;$filetoparse");
  
      $outputfile='./errorlog.txt';
      open(OUTPUT,"&gt;$outputfile");
      $errorstart='ERROR';
      $errorend=". ";
  
      while(&lt;FH&gt;)
      {
      if (FH=~ /^\s*$errorstart/../$errorend/)   
      {
          print OUTPUT "success";
      }   
      else
      {
          print OUTPUT "failed";
      }
      }
  
  }
  </code></pre>
  
  <p>the $errorstart and $errorend are something I saw online and am not sure if that is the correct way to code it.</p>
  
  <p>Also I know the printing "Success" or "Failure" is not what I said I am looking for, I added that in to help with confirmed that the code works, I haven't tried coding for counting the error messages yet.</p>
  
  <p>before this snippet of code I have a print statement asking the user for the location address of the .txt file they want to parse. I confirmed that particular section of code words properly. Thanks for any help! Let me know if more info is needed!</p>
  
  <p>Here is an example of data that I will be using:</p>
  
  <p>Sample Data</p>
  
  <p>-----BEGIN LOAD-----<br>
  SUCCESS: file loaded properly .<br>
  SUCCESS: file loaded properly .<br>
  SUCCESS: file loaded properly .<br>
  SUCCESS: file loaded properly .<br>
  SUCCESS: file loaded properly .<br>
  SUCCESS: file loaded properly .<br>
  ERROR: the file was unable to load for an unknown reason .<br>
  SUCCESS: file loaded properly .<br>
  SUCCESS: file loaded properly .<br>
  ERROR: the file was unable to load this is just an example of a log file that   will span<br>
  multiple lines .<br>
  SUCCESS: file loaded properly .<br>
  ------END LOAD-------  </p>
  
  <p>While the log may not necessarily NEED to span multiple lines, there will be some data throughout the log that will similar to how it is above. Every message logged starts with either SUCCESS or ERROR and the message is done when a " . " (whitespace-period-whitespace) is encountered. The log I want to parse through is 50,000 entries long so needless to say I would like to code so it will also identify multi-line error msgs as well as output the entire multi-line message to the output file.</p>
  
  <p><strong>update</strong></p>
  
  <p>I have written the code but for some reason it won't work. I think it has to do with the delimiter but I can't figure out what it is. The file I am using has messages that are separated by "whitespace period newline". Can you see what I'm doing wrong??</p>
  
  <pre><code>{
  local $/ = " .\n";
  if ($outputtype == 1)
  {
      $outputfile="errorlog.txt";
      open(OUTPUT,"&gt;&gt;$outputfile");
      $errorcount=0;
      $errortarget="ERROR";
      print OUTPUT "-----------Error Log-----------\n";
      {
      while(&lt;FH&gt;)
      {
      if ($_ =~ m/^$errortarget/)
      {
          print OUTPUT "$_\n";
          print OUTPUT "next code is: \n";
          $errorcount++;
      }
      }
      print OUTPUT "\nError Count : $errorcount\n";
      }
  }
  }
  </code></pre>