#! /usr/bin/perl
use strict; use warnings;
my %files=(); my @files=(); my $currentfile=''; my $maxcols=1;
while(<>) {
chomp;
# a hash such as %files is inherently unordered, so store each
# filename we process in @files, in the order that we see them.
if ($currentfile ne $ARGV) {
$currentfile = $ARGV ;
push @files, $currentfile;
};
# choose between the entire input line or the first 20 chars:
#push @{ $files{$currentfile} }, $_ ;
push @{ $files{$currentfile} }, substr($_,0,20) . '...';
# keep track of the largest number of columns in the %files
# hash-of-arrays. in other words, the largest number of lines in any
# input file.
if (@{ $files{$currentfile} } > $maxcols) {
$maxcols = @{ $files{$currentfile} }
};
};
print join("\t", map {"column$_"} @{[1..$maxcols+1]} ),"\n";
foreach my $f (@files) {
print join("\t",$f,@{ $files{$f} }),"\n";
}
Saída:
column1 column2 column3 column4
Article1 paragraph1 It is a l... paragraph2 It is a l... paragraph3 It is a l...
Article2 It is a long establi... It is a long establi... It is a long establi...
Article3 Lorem Ipsum is simpl... Lorem Ipsum is simpl... Lorem Ipsum is simpl...
Nota: a saída é separada por separadores. Os campos não se alinham visualmente com os títulos das colunas porque são mais longos que a largura da guia padrão.