여러 텍스트 파일을 하나의 CSV로 병합 [닫기]

여러 텍스트 파일을 하나의 CSV로 병합 [닫기]

입력 파일:

파일: 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(따옴표 제외)로 변환되고 파일은 한 줄로 변환됩니다.

",%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...

참고: 출력은 탭으로 구분됩니다. 이러한 필드는 기본 탭 너비보다 길기 때문에 열 머리글과 시각적으로 정렬되지 않습니다.

관련 정보