title: Parsing AutoSys JIL with perl
body: <p>I have an assignment to parse out AutoSys JIL files.  This is a JIL job definition, it is a config file that the AUTOSYS scheduler reads in and runs. , Imagine a file formatted like this, with thousands of job definitions like the one below, stacked on top of each other in the exact same format. All beginning with the header and ending with the timezone.</p>
  
  <pre class="lang-none prettyprint-override"><code>/* ----------------- COME_AND_PLAY_WITH_US_DANNY ----------------- */
  
  insert_job: COME_AND_PLAY_WITH_US_DANNY   job_type: CMD
  command: /bin/bash -ls
  machine: capser.com
  owner: twins
  permission: foo,foo
  date_conditions: 1
  days_of_week: mo,tu,we,th,fr
  start_times: &quot;04:00&quot;
  description: &quot;Forever, and ever and ever&quot;
  std_in_file: &quot;/home/room217&quot;
  std_out_file: &quot;${CASPERSYSLOG}/room217.out&quot;
  std_err_file: &quot;${CASPERSYSLOG}/room217.err
  alarm_if_fail: 1
  profile: &quot;/autosys_profile&quot;
  timezone: US/Eastern
  </code></pre>
  <p>This is the script. I need to extract the job, machine and command from the job definition above.  It works fine, but eventually I am going to want to store the information in some kind of container and send it, while this script writes out the results line by line in the terminal. Right now I am redirecting the results to a temporary file.</p>
  <pre class="lang-perl prettyprint-override"><code>#!/foo/bar/perl5/core/5.10/exec/bin/perl
  use strict;
  use warnings;
  use File::Basename ;
  
  my($job, $machine, $command)  ;
  my $filename = '/tmp/autosys.jil_output.padc';
  open(my $fh, '&lt;:encoding(UTF-8)', $filename)
    or die &quot;Could not open file '$filename' $!&quot;;
  my $count = 0;
  while (my $line = &lt;$fh&gt;) {
      #chomp $line;
      if($line =~ /\/\* -{17} \w+ -{17} \*\//) {
      $count = 1; }
      elsif($line =~  /(alarm_if_fail:)/) {
      $count = 0 ; }
      elsif ($count) {
               if ($line =~ m/insert_job: (\w+).*job_type: CMD/) {
               $job = $1   ;
               }
               elsif($line =~ m/command:(.*)/) {
               $command = $1  ;
               }
               elsif($line =~ m/machine:(.*)/) {
               $machine = $1  ;
  
               print &quot;$job\t $machine\t $command \n &quot;;      
               }
          }
  
  
      #sleep 1 ;
     }
  </code></pre>
  
  <p>My question is When I place the print <em> $job, $machine $command </em> statement within the last elsif statement, it works fine. However when I place it out side of the last elsif statement, like the example below the output is duplicated over and over again - each line is duplicated like four to five times in the output. I do not understand that. How come I have to put the print statement within the last elsif statement to get the script to print out one line at a time, correctly.</p>
  <pre class="lang-perl prettyprint-override"><code>elsif ( $line =~ m/machine:(.*)/ ) {
      $machine = $1;
  }
  
  print &quot;$job\t $machine\t $command \n &quot;;
  </code></pre>
  <br/>
  <hr />
  <h3>Reformat of above code for readability</h3>
  <pre class="lang-perl prettyprint-override"><code>#!/foo/bar/perl5/core/5.10/exec/bin/perl
  
  use strict;
  use warnings;
  
  use File::Basename;
  
  my ( $job, $machine, $command );
  my $filename = '/tmp/autosys.jil_output.padc';
  
  open( my $fh, '&lt;:encoding(UTF-8)', $filename )
          or die &quot;Could not open file '$filename' $!&quot;;
  
  my $count = 0;
  
  while ( my $line = &lt;$fh&gt; ) {
  
      #chomp $line;
      if ( $line =~ /\/\* -{17} \w+ -{17} \*\// ) {
          $count = 1;
      }
      elsif ( $line =~ /(alarm_if_fail:)/ ) {
          $count = 0;
      }
      elsif ( $count ) {
  
          if ( $line =~ m/insert_job: (\w+).*job_type: CMD/ ) {
              $job = $1;
          }
          elsif ( $line =~ m/command:(.*)/ ) {
              $command = $1;
          }
          elsif ( $line =~ m/machine:(.*)/ ) {
              $machine = $1;
              print &quot;$job\t $machine\t $command \n &quot;;
          }
      }
  
      # sleep 1;
  }
  </code></pre>