0

このフォーラムで見つけた、テキスト ファイルから列を抽出するためのこの Perl ワンライナーがあります。

perl -ane "print qq(@F[0,1,3,4]\n)" ./folder/input.txt 
    > ./short_files/output.txt

名前を持つ 352 個の入力ファイルでこれを実行し、名前input1.txt, input2.txt,...,input352.txtを持つ 352 個の出力ファイルを生成する必要がありますoutput1.txt, output2.txt,..., output352.txt

入力と出力のファイル名が毎回変わるループで Perl ワンライナーを実行できるように、ラッパー コードを手伝ってくれる人はいますか?

4

2 に答える 2

1

Perl ソリューションが必要なようです。

for my $in_file (glob('input*.txt')) {
   ( my $out_file = $in_file ) =~ s/input/output/;

   open(my $fh_in, '<', $in_file)
      or die("Can't open \"$in_file\": $!\n");

   open(my $fh_out, '>', $out_file)
      or die("Can't create \"$out_file\": $!\n");

   while (<$fh_in>) {
      my @F = split;
      print $fh_out "@F[0,1,3,4]\n";
   }
}
于 2012-08-08T19:35:46.723 に答える
0

あなたはUnixにいますか?ありますbashか?

for i in {1..352} ; do
    perl -ale ... ./folder/input$i.txt > ./short_files/output$i.txt
done
于 2012-08-08T19:15:01.950 に答える