in reply to text processing

The problem seems simple enough, but isn't specified completely enough for a complete solution that doesn't involve a bit of lucky guessing. Is there another record that comes after END, for example? Are the number of fields the same for each row? Are there always two rows per record?

At minimum, it does appear that you're dealing with fixed-width fields, and that you want to skip the first four lines. It's not clear to me what you want to have happen after "END" (continue on to a new record, or stop? And will that next record have its own headers? Will it have the same format as the first record?

For fixed-width fields, you might want to use unpack, as my @fields = unpack '(a7x)2a7', $_;, for example. This will have to come after whatever logic you use to disqualify some lines. That logic might look like this:

while( <DATA> ) { next if $. < 5; chomp; next if ! length; last if /^END/; my @fields = unpack '(a7x)2a7', $_; # Do something with the fields. }

This would change a bit if there are more than one record you're interested in. You might incorporate the flip-flop operator like this:

my $record_start = 0; my @recs; while( <DATA> ) { chomp; if( /^TABLE NAME/ .. /^END/ ) { # We're in a new record... if( /^TABLE NAME/ ) { $record_start = $.; push @recs, []; } next unless $. > $record_start + 3; # Skip header. next if ! length; next if /^END/; my @fields = unpack '(a7x)2a7', $_; # Do something with fields, such as... push @{$recs[-1]}, [@fields]; } }

(Updated to demonstrate pushing records onto a "@recs" array.)


Dave

Replies are listed 'Best First'.
Re^2: text processing
by locked_user sundialsvc4 (Abbot) on Apr 23, 2014 at 02:15 UTC

    Actually, David, in this case I do believe that there’s enough information here to point to a classic, awk-inspired solution.   The “set of records of-interest” is clearly bounded by an identifiable “start” and “end” record, and, within that space, the set of records which contain information-of-interest are readily identifiable.   Thus, logic could be written, I think, based only on the file-example presented in the original post.   And this logic would basically be in-keeping with the metaphor that the awk tool already employs.   (Which means, of course, that a very short Perl program could also do the same.)