#! perl -slw use strict; ## The A template takes care of trailing spaces my $inTempl = 'A8 x1 A9 x1 A8 A*'; my @headers = split ' ', ; ## Are these headers used? my( @output, $accum ); while( my $line = ) { 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\