Combinando vários arquivos de texto em um único csv [closed]

0

Arquivos de entrada:

Arquivo: 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......

Arquivo: 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......

Arquivo: 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.......

Saída desejada:

Arquivo: 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....... 
    
por Zilvia Smith 30.05.2016 / 06:19

3 respostas

1

apenas um palpite selvagem

 awk 'BEGINFILE { printf "%s",FILENAME}
                { printf ",%s",$0 ;}
      ENDFILE { printf "\n" ;}' file1.txt file2.txt file3.txt

isto irá transformar o arquivo em csv (mas sem aspas), sendo o arquivo convertido em uma linha.

substitua ",%s" por "\t%s" para usar a guia.

    
por 30.05.2016 / 08:34
0

Primeiro, combine todos os arquivos de texto:

cat Article1.txt Article2.txt Article3.txt > Result.txt

Em seguida, converta o arquivo de texto em CSV:

 (echo "Col1;Col2;Col3" ; cat Result.txt) | sed 's/;/<tab>/g' > file.csv
    
por 30.05.2016 / 07:09
0
#! /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.

    
por 30.05.2016 / 08:39