Going by the spelling error "bananaswiithapples.gif", I assume that you typed the sample in by hand rather than C&Ping from the real data. At least I hope you did because your records are inconsistant.
In the first record, the third field is wrapped at 8 chars. In the second record, the first two lines of that third field are wrapped at 8 chars and the last line at 9. In the third record, the first two lines of the third field are wrapped at 9 and the last line extends to 10.
There are similar inconsistacies in the wrapping of the second field/first record. I've adjusted the data to fit my assumption and I apologies if it is wrong.
This determines the output formatting by finding the maximum width of each field. It assumes that you have enough memory to accumulate all the records in memory. Otherwise it would be necessary to do two passes through the file:
#! perl -slw use strict; ## The A template takes care of trailing spaces my $inTempl = 'A8 x1 A9 x1 A8 A*'; my @headers = split ' ', <DATA>; ## Are these headers used? my( @output, $accum ); while( my $line = <DATA> ) { chomp $line; my @bits = unpack $inTempl, $line; s[^\s*][]g for @bits; ## Trim leading spaces if( $line =~ m[^\S] ) { ## Start of a new record. ## Add to the list push @output, $accum if $accum; ## Start a new accumulation $accum = \@bits; } else { ## Append to the accumulators $accum->[ $_ ] .= $bits[ $_ ] for 0 .. $#bits; } } push @output, $accum; ## Don't forget the last record. ## Determine the output field widths my @w = ( 0 ) x 4; for my $ref ( @output ) { for my $i ( 0 .. 3 ) { my $len = length( $ref->[ $i ]||'' ); $w[ $i ] = $len if $w[ $i ] < $len; } } ## Build an output template with a extra space between fields my $outTempl = join ' ', map 'A' . ($_+1), @w; ## And output print pack $outTempl, @$_ for @output; __DATA__ NodeName FileName PathName BackupDate BD3101 bananaswi \breakfa 2007-03-06 ithapple st\fruit 14:02:31.000000 s.gif s\tree\ TP4223 chocolate \sweet\d 2006-02-28 caramelfu esserts\ 21:16:41.000000 dge.gif hershey\ EO2123 tofuwith \organic 2007-07-16 peas.gif \vegetab 13:55:06.000000 les\legu mes\
Produces
C:\test>632548 BD3101 bananaswiithapples.gif \breakfast\fruits\tree\ 2007-03- +0614:02:31.000000 TP4223 chocolatecaramelfudge.gif \sweet\desserts\hershey\ 2006-02- +2821:16:41.000000 EO2123 tofuwithpeas.gif \organic\vegetables\legumes\ 2007-07- +1613:55:06.000000
In reply to Re: MultiLine Tables into Variables
by BrowserUk
in thread MultiLine Tables into Variables
by Knoperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |