Text::CSV
는 CSV를 구문 분석하는 데 사용할 수 있습니다. 다음 스크립트는 CSV 파일이 들어있는 디렉토리에서 실행됩니다. 재귀 적 (glob
사용)입니다. 파일을 재귀 적으로 찾길 원하는 경우 File::Find
Perl 모듈을 사용할 수 있습니다.
#!/usr/bin/env perl
use strict;
use warnings;
use Text::CSV;
my $csv = Text::CSV->new({ 'sep_char' => ';' });
open my $fho, '>', 'combined.csv' or die "Error opening file: $!";
while (my $file = <*.csv>) {
open my $fhi, '<', $file or die "Error opening file: $!";
(my $last_field = $file) =~ s/\.[^\.]+$//; # Strip the file extension off
while (my $row = $csv->getline($fhi)) {
$csv->combine(@$row, $last_field); # Construct new row by appending the file name without the extension
print $fho $csv->string, "\n"; # Write the combined string to combined.csv
}
}
매우 직설적입니다. 너 뭐 해봤 니? – delicateLatticeworkFever
명백하게 묻는 것에 대해 유감스럽게 생각합니다. 그러나 PERL 프로그래밍에 익숙하지 않고 그의 대답에 대해 Alan에게 감사드립니다. – Steve06