http://www.perlmonks.org?node_id=1003891


in reply to Using perl to extract data from multiple .txt files

Hi, lalalala1, and welcome to PerlMonks!

Excellent suggestions above. The following incorporates almost all of them, and avoids a collision with the output file by grepping the glob (sounds like a horror movie title...).

It opens each text file for the data you need, and that data is pushed onto an array that's written out to the output file:

use strict; use warnings; my $outFile = 'extractedData.txt'; my @sampleLines; for my $file ( grep !/^\Q$outFile\E$/, <*.txt> ) { open my $fh, '<', $file or die $!; # Get data from file... push @sampleLines, $data; close $fh; } open my $outFH, '>', $outFile or die $!; print $outFH @sampleLines; close $outFH

I know that this is more than you requested, but I hope it's helpful.

Replies are listed 'Best First'.
Re^2: Using perl to extract data from multiple .txt files
by lalalala1 (Initiate) on Nov 15, 2012 at 14:27 UTC

    Thank you guys, I'll give your suggestions a try and let you know how it worked out.