use warnings; use strict; use Text::CSV; my $csv = Text::CSV->new; my $file = 'in.csv'; my $out1 = 'out.1'; my $out2 = 'out.2'; open my $fh, '<', $file or die "can't open the damned csv file!: $!"; my %widths; while (my $row = $csv->getline($fh)){ for (0..$#$row){ my $length = length $row->[$_]; if (! $widths{$_} || $length > $widths{$_}){ $widths{$_} = $length; } } } seek $fh, 0, 0; open my $wfh_1, '>', $out1 or die "can't open the bloody output file $out1!: $!"; open my $wfh_2, '>', $out2 or die "can't open the dirty output file $out2!: $!"; my @header; my $got_header; while (my $row = $csv->getline($fh)){ @header = @$row if ! $got_header; $got_header = 1; for (0..$#$row){ if (@header){ print $wfh_2 "$header[$_] : $row->[$_]\n"; } my $elem_len = $widths{$_} - length($row->[$_]); $row->[$_] .= ' ' x $elem_len; } print $wfh_1 join(' ', @$row); print $wfh_1 "\n"; } close $fh; close $out1; close $out2;