• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

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



0
ISGDude
Asked:
ISGDude
2 Solutions
 
Adam314Commented:
You could use a regex like   /Creating/../^-/
0
 
ghostdog74Commented:
to get next 5 lines after pattern

while (<>) {
    print $_ if $c && $c--;;
    if (/Creating/) {
        $c = 5;
    }
}
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now