For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > Process Multiple Files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Process Multiple Files
banker123

2006-10-31, 6:58 pm

How do I process multiple files within a directory using Perl.

Example:
Directory c:\files
103106_1.txt
103106_2.txt

contents of file 1
123456 abcdef test

contents of file 2
789112 abcdef test

I woul like to while through each file and extract the first 6 digits
from each file. Alos the file name changes each day to contain the
current date.

DJ Stunks

2006-10-31, 6:58 pm

banker123 wrote:
> How do I process multiple files within a directory using Perl.
>
> Example:
> Directory c:\files
> 103106_1.txt
> 103106_2.txt
>
> contents of file 1
> 123456 abcdef test
>
> contents of file 2
> 789112 abcdef test
>
> I woul like to while through each file and extract the first 6 digits
> from each file. Alos the file name changes each day to contain the
> current date.


think about the steps you need to take...

1) get a list of files in the directory. you could use readdir() or
glob for this
2) foreach file in the list, open the file. use open() for this.
3) once you are sure that you were able to open the file ok, read the
first line of the file. use readline() for this
4) grab the first 6 digits from the first line - use a regular
expression, or substr
5) close the file and move on to the next - close()

if you have questions about any of these functions, use the perl
documentation to learn more about them, for instance

perldoc -f open

give this a try, put it all together, and if you still have problems,
write back to the list and show us what you tried, what it did, and
what you wanted it to do differently.

-jp

banker123

2006-10-31, 9:57 pm

>read the first line of the file. use readline() for this

I am stuck I am not familiar with the readline() and do not know how to
procede.

I am reaserching readline(). In the meantime if anyone can offer some
help it is certainly appreciated, maybe even an explanation of readline
would help, I know I can pull up the perldoc but sometimes an
explanation from folks in this group are easier to understand.

banker123

2006-10-31, 9:57 pm

> I am stuck I am not familiar with the readline() and do not know how to
> procede.


Got readline working, how do I specify which $line? Example only lines
that contains xxxx.
I assume I would use regex?

How do I use substr to extract the data? After specifying $lines that
contain xxxx how do I use substr to parse the string?

my $dir="C:/data/";
opendir DH, $dir or die "Cannot open$!";
@files=grep(/\.txt$/,readdir(DH));
closedir (DH);

foreach $file (@files) {
open (data, "$dir$file") or die "Cannot open file: $!";
$line = readline(data);
print "$line";
close (data);
}

banker123

2006-10-31, 9:57 pm

Is this the most effecient approach to accomplish the ojetcive stated
in the first post?

> Got readline working, how do I specify which $line? Example only lines
> that contains xxxx.
> I assume I would use regex?
>
> How do I use substr to extract the data? After specifying $lines that
> contain xxxx how do I use substr to parse the string?
>

my $dir="C:/data/";
opendir DH, $dir or die "Cannot open$!";
@files=grep(/\.txt$/,readdir(DH));
closedir (DH);

foreach $file (@files) {
open (data, "$dir$file") or die "Cannot open file: $!";
while ( <data> ) {
if ( /xxxx/ ) {
my $data = substr($_,4,12);
print "$data\n";
}}
}

DJ Stunks

2006-10-31, 9:57 pm

banker123 wrote:
> Is this the most effecient approach to accomplish the ojetcive stated
> in the first post?
>
> my $dir="C:/data/";
> opendir DH, $dir or die "Cannot open$!";
> @files=grep(/\.txt$/,readdir(DH));
> closedir (DH);
>
> foreach $file (@files) {
> open (data, "$dir$file") or die "Cannot open file: $!";
> while ( <data> ) {
> if ( /xxxx/ ) {
> my $data = substr($_,4,12);
> print "$data\n";
> }}
> }


it looks fine to me, does it work? does it do what you wanted?

as for efficiency, that's probably close to as efficient as it gets.
since you're using a regular expression in your if, you can probably
use capturing parenthesis to grab your $data at that stage rather than
having to substr into the line, but for a first script I'd say you've
done pretty well.

-jp

banker123

2006-11-01, 7:58 am

Great thanks for the help along the way.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com