入力ファイル:
ファイル: Article1.txt:
paragraph1 It is a long established fact that a reader will......
paragraph2 It is a long established fact that a reader will......
paragraph3 It is a long established fact that a reader will......
ファイル: Article2.txt:
It is a long established fact that a reader will......
It is a long established fact that a reader will......
It is a long established fact that a reader will......
ファイル: Article3.txt:
Lorem Ipsum is simply dummy text of the printing.......
Lorem Ipsum is simply dummy text of the printing......
Lorem Ipsum is simply dummy text of the printing.......
希望の出力:
ファイル: example.csv:
column1 column2 column3
Article1 paragraph1 It is a...... paragraph2 It is a.......
Article2 paragraph1 It is a...... paragraph2 It is a.......
Article3 Lorem I....... Lorem I.......
答え1
ただ奇妙な推測だけです。
awk 'BEGINFILE { printf "%s",FILENAME}
{ printf ",%s",$0 ;}
ENDFILE { printf "\n" ;}' file1.txt file2.txt file3.txt
これにより、ファイルはcsv(引用符を除く)に変換され、ファイルは1行に変換されます。
",%s"
タブを使って交換します"\t%s"
。
答え2
まず、すべてのテキストファイルをマージします。
cat Article1.txt Article2.txt Article3.txt > Result.txt
その後、テキストファイルをCSVに変換します。
(echo "Col1;Col2;Col3" ; cat Result.txt) | sed 's/;/<tab>/g' > file.csv
答え3
#! /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";
}
出力:
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...
注:出力はタブで区切られます。これらのフィールドはデフォルトのタブ幅よりも長いため、列見出しと視覚的に並べ替えられません。