On 1/19/10 Tue  Jan 19, 2010  3:03 PM, "Perl Noob"
<[email protected]> scribbled:


> 
> The data is not consistent throughout the entire file.  I WISH I only
> had to skip every other line.  The problem is not quite that simple.
> The data I need is always consistent within the file, but is not so
> neat as to be on every other line.  The common characteristic of the
> data I need is that the record has an end of line marker followed by
> 65 spaces on the following line.  Here is a better sample of what I
> described:
> 
> _______BEGIN SAMPLE DATA FILE_________________
> RandomJunkNothingImportantMoreJunk
> StuffthatdoesntmatterWhocaresaboutthis
> RECORD1FIELD1(3 spaces)RECORD1FIELD2(3 spaces)RECORD1FIELD3(newline)
> (65 spaces)RECORD1FIELD4(12 spaces)RECORD1FIELD5
> RECORD2FIELD1(3 spaces)RECORD2FIELD2(3 spaces)RECORD2FIELD3(newline)
> (65 spaces)RECORD2FIELD4(12 spaces)RECORD2FIELD5
> RandomJunkNothingImportantMoreJunk
> StuffthatdoesntmatterWhocaresaboutthis
> MoreJunkThatDoesntmatterStuffIdontwantWhocaresaboutthis
> RECORD3FIELD1(3 spaces)RECORD3FIELD2(3 spaces)RECORD3FIELD3(newline)
> (65 spaces)RECORD3FIELD4(12 spaces)RECORD3FIELD5
> RECORD4FIELD1(3 spaces)RECORD4FIELD2(3 spaces)RECORD4FIELD3(newline)
> (65 spaces)RECORD4FIELD4(12 spaces)RECORD4FIELD5
> RECORD5FIELD1(3 spaces)RECORD5FIELD2(3 spaces)RECORD5FIELD3(newline)
> (65 spaces)RECORD5FIELD4(12 spaces)RECORD5FIELD5
> RECORD6FIELD1(3 spaces)RECORD6FIELD2(3 spaces)RECORD6FIELD3(newline)
> (65 spaces)RECORD6FIELD4(12 spaces)RECORD6FIELD5
> ___________END SAMPLE DATA FILE ____________________
> 
> 
> You will notice in the sample above that the only consistent items
> between the usable data is the (newline) followed by (65 spaces).
> Therefore if I could find a way to do a search and replace
> 
> s/(newline)(65spaces)//gi;
> 
> that would be great.  I just need to get each (newline)followed by
> (65spaces) and delete it.  I just am not sure how to do that.  My
> brain hurts.
> 

The Perl readline operator (<>) returns lines one-at-a-time. Since you wish
to combine one line with the next line only if the second line starts with
65 spaces, you will have to save each line and not print it until the
following line has been read. Something like (untested):

    my $prev = '';
    while( my $line = <DATA> ) {
      if( $line =~ /^\s{65}/ ) {
        chomp($prev);
        print "$prev$line";
        $prev = '';
      }else{
        print $prev if $prev;
        $prev = $line;
      }
    }
    print $prev if $prev;   # print last line


You might be able to speed this up by using references, so you don't have to
copy scalars ($prev = $line).



-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to