2013-08-01 147 views
-1

我是Perl的新手,所以對於簡單的問題感到抱歉。使用Perl合併多個csv文件

我有一個產生7個CSV文件的Perl腳本。所有這些文件都有8個共同的列標題。

的文件名稱將保持不變,每個文件只有8列,總是會有在每列值數據。

每個文件大小決不會小於400K大。

我想這些CSV文件合併成使用Perl一個文件。輸出將具有與所有7個文件相同的列標題和數據。

+0

所以,你只是想追加的文件,但刪除頁眉在?之間? – amon

+3

爲什麼不編輯主Perl腳本來打印到一個文件而不是7個不同? – TLP

+0

阿蒙 - 是的,只是追加和刪除標題 – user2641999

回答

2

如果你是在某種Unix的,你可以使用tail

$ tail -qn +2 fileA fileB ... 

-q supresses filenames in the output;第二行開始輸出-n +2

要獲得頭以及:

$ (head -n 1 fileA; tail -qn +2 fileA fileB ...) > output-file 

如果你需要使用Perl:

use strict; use warnings; use autodie; 
my $files = $#ARGV; # get number of files - 1 
while (my $file = shift @ARGV) { 
    open my $fh, "<", $file; 
    <$fh> unless $files == @ARGV; # discard header unless first file 
    print while <$fh>; # output the rest 
} 

然後:$ perl the-script.pl fileA fileB ...