470,591 Members | 1,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

Perl Script for Parsing

Hi,

I am looking out for a Perl script to do the following. I am quite
positive someone must have written something similar.

- A perl script to open a directory/folder and access all files and
directories(and files within it)after a particular time stamp.

- Then for each file with a particular string in its name, access the
file and look for string
':20C::RELA//49879'

- Based upon that do throw an error or proceed for some other tasks.
I don't have the details yet from my manager, but I figure this script
will be run as a cron - every night.
I know PERL is pretty nifty for such tasks, that's why I feel
confident that someone must have written a script
already.

I did look at CPAN, but it is overwhelming. Still looking though.

Thanks,
- v796.
Jul 19 '05 #1
2 4887
v796 wrote:
I am looking out for a Perl script to do the following. I am quite
positive someone must have written something similar.

- A perl script to open a directory/folder
perldoc -f opendir
perldoc -f readdir
and access all files and directories
perldoc perlsyn (in particular the for and while loops)
perldoc -f open
(and files within it)
Ah, so you want it recursive? Why didn't you say so from the beginning?
Forget about opendir, readdir, while, and for, just use

perldoc File::Find
after a particular time stamp.
perldoc -f -M
- Then for each file with a particular string in its name,
perldoc -f index
access the
file and look for string
':20C::RELA//49879'
perldoc -f open
perldoc -f index
- Based upon that do throw an error or proceed for some other tasks. I did look at CPAN, but it is overwhelming. Still looking though.


Good idea, but including error checking this is probably 15-20 lines of code
max. I doubt that you will find something this simple on CPAN.

jue
Jul 19 '05 #2
> Good idea, but including error checking this is probably 15-20 lines of code
max. I doubt that you will find something this simple on CPAN.


Everybody writes this all the time. Today's implementation (untested):

#/usr/bin/perl
use strict;
use warnings;
use File::Find;

my $root_dir = '/etc';
my $time_stamp = time - 24 * 60 * 60; # last 24 hours
my $regex = qr':20C::RELA//49879'; # your regex
my @candidates;

find(
sub {

my $cur_file = $File::Find::name;
return unless -f $cur_file && (stat($cur_file))[9] >= $time_stamp;

# opportunity for improvement here: the error does not need to be fatal
open my $FILE, $cur_file or die "Cannot open file $cur_file\n";

READLINE: while (<$FILE>) {
push(@candidates, $cur_file), last READLINE if /$regex/;
}

},

$root_dir
);
{
local $, = "\n";
print @candidates;
}

__END__

Regards, Roel
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by John Smith | last post: by
52 posts views Thread by Olivier Scalbert | last post: by
3 posts views Thread by John Smith | last post: by
reply views Thread by Antwerp | last post: by
5 posts views Thread by Robert Oschler | last post: by
3 posts views Thread by sainiamit25 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.