perl.pl (684B)
1 #!perl -w 2 3 # Time-stamp: <2002/04/06, 13:12:13 (EST), maverick, csvformat.pl> 4 # Two pass CSV file to table formatter 5 6 $delim = $#ARGV >= 1 ? $ARGV[1] : ','; 7 print STDERR "Split pattern: $delim\n"; 8 9 # first pass 10 open F, "<$ARGV[0]" or die; 11 while(<F>) 12 { 13 chomp; 14 $i = 0; 15 map { $max[$_->[1]] = $_->[0] if $_->[0] > ($max[$_->[1]] || 0) } 16 (map {[length $_, $i++]} split($delim)); 17 } 18 close F; 19 20 print STDERR 'Field width: ', join(', ', @max), "\n"; 21 print STDERR join(' ', map {'-' x $_} @max); 22 23 # second pass 24 open F, "<$ARGV[0]" or die; 25 while(<F>) 26 { 27 chomp; 28 $i = 0; 29 map { printf("%-$max[$_->[1]]s ", $_->[0]) } 30 (map {[$_, $i++]} split($delim)); 31 print "\n"; 32 } 33 close F;