file parsing a block of data

What is the cleanest way to parse a block of data from a file.

I need to find the word "Creating" in the file and copy that line along with the next 5 lines to a report file.

I'm finding a lot of different ways to do this, but I know there must be a clean and simple way of doing this.

After this, I need to continue saving lines that do not end with "0" until the start of the line equals "-".

Thanks,
Randy



ISGDudeAsked:
Who is Participating?
 
ghostdog74Commented:
to get next 5 lines after pattern

while (<>) {
    print $_ if $c && $c--;;
    if (/Creating/) {
        $c = 5;
    }
}
0
 
Adam314Commented:
You could use a regex like   /Creating/../^-/
0
 
nedfineCommented:
open IN, "test.txt";
open OUT , ">create.txt";
$flag1 =1;$flag =0;
while(<IN>)
{
 if($_=~/Creating/)
 {
  $flag =1;
  $i=0;
 }
 if($flag ==1 && $i<5)
 {
  print OUT "$_";
  $i++;
 }
 else
 {
  if($_!~/0$/)
  {
    if($flag1==1 && $flag==1)
      {
        print OUT "$_";
      }
      else
      {
       if($_=~/^\-/)
       {
         $flag1 =0;
       }
      }
  }
 }
}
0
 
ISGDudeAuthor Commented:
Adam314:
I got your regex to work.
Could it be modified it to skip over the 1st ^- it finds and select the 2nd instance?  

ghostdog74:
You're code worked also,  It actually got me on the right track to get Adam314's to work as well as future parsing that I need to do.

I don't understand the need for the double ;; at the end of this line:
   print $_ if $c && $c--;;
I read this line as saying:      print if $c exists (or true)  and decrement $c
Is the 1st semicolon for the dec function and the 2nd for the if statement?

Also, it seems backward to me that you check for /Creating/ and set $c after the print statement.
Seems like it should skip the line containing "Creating" in this case.

Some of the stuff Perl does seems "magical" not logical.

Thanks for your help,
R.


0
 
ghostdog74Commented:
>>>I don't understand the need for the double ;; at the end of this line:
   >>> print $_ if $c && $c--;;

you can remove the extra colon.just typo error
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.