These txt files are in groups of 25 (in folders). My goal is to automate this so that after specifying a folder, Perl will pull the specified lines from each of the 25 txt files in that folder and write them to a single output file.
I would greatly appreciate any help or suggestions!
Bryan
-------------
Bryan Keller, Doctoral Student/Project Assistant
Educational Psychology - Quantitative Methods
The University of Wisconsin - Madison
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: I"m new to Perl and I"m stuck
am 03.03.2009 19:29:57 von Gaurav Vaidya
Hi Bryan,
On Mar 4, 2009, at 1:27 AM, Bryan Keller wrote:
> These txt files are in groups of 25 (in folders). My goal is to
> automate this so that after specifying a folder, Perl will pull the
> specified lines from each of the 25 txt files in that folder and
> write them to a single output file.
The quick-and-dirty way of doing that would be to use the glob
operator (documented at http://perldoc.perl.org/functions/glob.html).
It's how you ask Perl to expand wildcards - so in Windows/DOS terms,
it's like doing "dir *.*", except in Perl :-). You can read more about
glob expansions at http://en.wikipedia.org/wiki/Glob_(programming).
You'll also need to modify your program to read from files, using open
(there's a nice tutorial at http://perldoc.perl.org/perlopentut.html).
If I were modifying your program, here's how I'd do it:
--program.pl begins
#!/usr/bin/perl
use warnings;
use strict;
my @files_to_process = glob("FolderName/*.txt"); # Or "*.txt" if you
want to process all the text files in the current folder
# @files_to_process is now an array containing a list of
# all files which match the pattern you specified - all
# files with a "txt" extension in the folder "FolderName",
# in my example above.
foreach my $filename (@files_to_process) {
open(INPUT, '<', $filename) or die "Could not open $filename: $!";
Standard disclaimers about programs thrown together at 2am apply, but
this program *seems* to run on my computer without any problems.
I'm sure there are more efficient ways of reading particular lines out
of a file, but I don't know any. I'm sure someone who does will post
it shortly :-), and reading the whole file into memory like you're
doing should work fine on small numbers of small files. You could also
ask this question at the Perl Beginners mailing list (at http://learn.perl.org/faq/beginners.html)
, who I imagine would know plenty o beginner-targeted resources to get
you started on this sort of file manipulation.
Hope that helps!
cheers,
Gaurav
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: I"m new to Perl and I"m stuck
am 04.03.2009 03:28:44 von Bill Luebkert
Bryan Keller wrote:
> Hi all,
>
> The following gets me the lines I need for any single txt file.
>
> #!usr/bin/perl
> use warnings;
> use strict;
>
> my @output = <>;
> print "$ARGV";
> print ("\nESTIMATION\n");
> print @output[18,21..24,28..31,206..208];
> print "\nPOPULATION\n";
> print @output[220,223..226,229..233,424..426];
>
> These txt files are in groups of 25 (in folders). My goal is to automate this so that after specifying a folder, Perl will pull the specified lines from each of the 25 txt files in that folder and write them to a single output file.
>
> I would greatly appreciate any help or suggestions!
Are you able to specify the files using wildcards on the commandline ?
If so, you could do it that way or you could select which ones you
want inside the script - eg (untested):
use strict;
use warnings;
my $dir = 'some path here'; # dir to peruse
my $ofile = 'some path here'; # output file
opendir DIR, $dir or die "opendir $dir: $! ($^E)";
my @files = readdir DIR;
closedir DIR;
open OUT, $file or die "create $ofile: $! ($^E)";
foreach my $file (@files) {
next if not /some reg exp criteria here/; # drop inappropriate files
open IN, $file or do {
warn "open $file: $! ($^E)"; # or die if you like
next;
};
my @content = ; # slurp file
print "Doing $file\n"; # debug
# your orig code more or less
print OUT "ESTIMATION\n";
print OUT @content[18,21..24,28..31,206..208];
print OUT "\n";
print OUT "POPULATION\n";
print OUT @content[220,223..226,229..233,424..426];
print OUT "\n";
}
close OUT;
__END__
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs