Can Perl do this?

C

CountryLover

First off, I know next to nothing about Perl, other than a few scripts that I
have used (written by others).

A project that I am working on requires the following steps:

1. Parse a directory containing text files, potentially thousands
2. Open each text file and read in the first 25 lines or so
3. Write these lines to a temporary file
4. Delete each original file & replace with the temp file, using same filename
5. Parse the truncated files and find a specific keyword
6. Create directories based on the value of the keywords found
7. Move the truncated files to their respective directories.
8. Optionally, create a log file of all operations

Is this something that Perl could be persuaded to do?

Any suggestions or code snippets would be much appreciated!
 
V

Vlad Tepes

CountryLover said:
First off, I know next to nothing about Perl, other than a few scripts that I
have used (written by others).

A project that I am working on requires the following steps:

1. Parse a directory containing text files, potentially thousands

my @files = said:
2. Open each text file and read in the first 25 lines or so
3. Write these lines to a temporary file
4. Delete each original file & replace with the temp file, using same filename

FILE: foreach my $filename ( @files ) {
open $fh, $filename or do {
warn "Can't open $filename: $!";
next FILE;
};
while ( <$fh> ) {

}
 
C

CountryLover

FILE: foreach my $filename ( @files ) {
open $fh, $filename or do {
warn "Can't open $filename: $!";
next FILE;
};
while ( <$fh> ) {

}

Thanks for the reply, Vlad, I really appreciate it. I've been doing alot of
this manually for several weeks, and it's gettin old fast :)

I'll try out your suggestions tonite.

Thanks again!
 
J

James E Keenan

CountryLover said:
First off, I know next to nothing about Perl, other than a few scripts that I
have used (written by others).

A project that I am working on requires the following steps:

1. Parse a directory containing text files, potentially thousands
2. Open each text file and read in the first 25 lines or so
3. Write these lines to a temporary file
4. Delete each original file & replace with the temp file, using same filename
5. Parse the truncated files and find a specific keyword
6. Create directories based on the value of the keywords found
7. Move the truncated files to their respective directories.
8. Optionally, create a log file of all operations

Is this something that Perl could be persuaded to do?
Yes.


Any suggestions or code snippets would be much appreciated!
Buy a good introductory Perl textbook such as Randal Schwartz's "Learning
Perl." Work through it. By the time you're 2/3 of the way through it,
you'll know enough Perl to accomplish everything you describe above.
 
C

CountryLover

Parse as "traverse"? Sure. Hmmm, with just one directory it means
getting all text files.


Yes, quite easy. I do similar things often with Perl :)

Probably a *nix hacker could do most, if not all with GNU tools like
find (the txt files), head (to get the first 25), mv, grep/sed, mkdir,
mv and such. Probably in a one-liner.


:) I could be persuaded to write it for you ;-)

Would a "pretty please" be sufficient to persuade you? :)

Thing is, I'm on a tight timeline, and starting with some ready-made code would
sure beat starting from scratch to learn Perl.

Pretty Please?!
 
C

CountryLover

(
Sorry about my previous reply. I started writing an answer, but
changed my mind. I really have to go to bed, and in my tired state
I pressed the wrong button. :-(
)

No prob, Vlad. I appreciate your effort nonetheless.

Have a good nite!
 
J

Jeff 'japhy' Pinyan

1. Parse a directory containing text files, potentially thousands
2. Open each text file and read in the first 25 lines or so
3. Write these lines to a temporary file
4. Delete each original file & replace with the temp file, using same filename

That can be done in Perl without a temporary file. Roughly:

open FILE, "<+ $filename" or
5. Parse the truncated files and find a specific keyword

seek FILE, 0, 0;
while (<FILE>) {
# look for keywords
}

That could even be done in the previous chunk of code, stopping at the
25th line and truncating.
6. Create directories based on the value of the keywords found
7. Move the truncated files to their respective directories.

Do you mean that you want a copy of each file in each directory that the
file has the keyword of? So that foo.txt, with keywords 'yucca', 'root',
and 'beer' would be in the yucca/, root/, and beer/ directories?
8. Optionally, create a log file of all operations

Simple.
 
M

Mike Flannigan

Perl would be great to do what you want. Problem is, you are
unlikely to get this done just by using somebody else's code.
I mean, you are probably going to need to upgrade and refine
the program as you use it and see it's shortcomings.

Simple things can often be done just by asking for an
answer on this list. In your case I highly recommend
taking the time to learn Perl. It will probably take you
only 2 or 3 weeks or so - maybe less.


Mike
 
C

CountryLover

Perl would be great to do what you want. Problem is, you are
unlikely to get this done just by using somebody else's code.
I mean, you are probably going to need to upgrade and refine
the program as you use it and see it's shortcomings.

Simple things can often be done just by asking for an
answer on this list. In your case I highly recommend
taking the time to learn Perl. It will probably take you
only 2 or 3 weeks or so - maybe less.

Agreed. However, I just don't have alot of time to devote to it right now on
top of the stuff they pay me to do. :)

I'm going to grab a few books and see what I can whip up....
 
J

John W. Krahn

CountryLover said:
First off, I know next to nothing about Perl, other than a few scripts that I
have used (written by others).

A project that I am working on requires the following steps:

1. Parse a directory containing text files, potentially thousands
2. Open each text file and read in the first 25 lines or so
3. Write these lines to a temporary file
4. Delete each original file & replace with the temp file, using same filename
5. Parse the truncated files and find a specific keyword
6. Create directories based on the value of the keywords found
7. Move the truncated files to their respective directories.
8. Optionally, create a log file of all operations

Is this something that Perl could be persuaded to do?

Any suggestions or code snippets would be much appreciated!


#!/usr/bin/perl
use warnings;
use strict;

# UNTESTED !!

my $log_file = '/home/cl/log_file';
open my $log, '>>', $log_file or die "Cannot open $log_file: $!";
select( ( select( $log ), $| = 1 )[ 0 ] );

my $dir = '/home/cl/textfiles';
my $lines_to_read = 25;

opendir my $dh, $dir or die "Cannot open $dir: $!";

while ( defined( my $file = readdir $dh ) ) {
next unless -T "$dir/$file";
print $log "Found file: $dir/$file\n";

open my $fh, '<', "$dir/$file" or die "Cannot open $dir/$file: $!";
my ( $data, $keyword );
while ( <$fh> ) {
last if $. > $lines_to_read;
$keyword = $1 if /keyword: (\w+)/;
$data .= $_;
}
close $fh;

next unless defined $keyword;
print $log "Found keyword: $keyword\n";

unless ( -d "$dir/$keyword" ) {
mkdir "$dir/$keyword" or die "Cannot mkdir $dir/$keyword: $!";
}
open my $fh, '>', "$dir/$keyword/$file" or die "Cannot open $dir/$keyword/$file: $!";
print $fh $data or die "Cannot write to $dir/$keyword/$file: $!";
close $fh;
print $log "New file $dir/$keyword/$file created.\n";

unlink "$dir/$file" or warn "Cannot delete $dir/$file: $!";
}

__END__



John
 
H

Helgi Briem

A project that I am working on requires the following steps:

1. Parse a directory containing text files, potentially thousands

perldoc perlrun
perldoc warnings
perldoc strict
perldoc -f opendir
perldoc -f grep
perldoc -f readdir
perldoc -f die
perldoc perlvar ($!)
perldoc File::Find (if there are subdirectories)
2. Open each text file and read in the first 25 lines or so

perldoc -f open
perldoc perlvar ( $. )
perldoc perlsyn (while)
3. Write these lines to a temporary file

perldoc -f open
perldoc -f print
perldoc -f close
4. Delete each original file & replace with the temp file, using same filename

perldoc -f unlink
5. Parse the truncated files and find a specific keyword

perldoc perlre (or perldoc perlrequick)
6. Create directories based on the value of the keywords found

perldoc -f mkdir
7. Move the truncated files to their respective directories.

perldoc -f rename
8. Optionally, create a log file of all operations

perldoc -f open
perldoc -f print
Is this something that Perl could be persuaded to do?

Of course. What have you tried so far?
 
B

Bart Lateur

CountryLover said:
First off, I know next to nothing about Perl, other than a few scripts that I
have used (written by others).

A project that I am working on requires the following steps:

1. Parse a directory containing text files, potentially thousands
2. Open each text file and read in the first 25 lines or so
3. Write these lines to a temporary file
4. Delete each original file & replace with the temp file, using same filename
5. Parse the truncated files and find a specific keyword
6. Create directories based on the value of the keywords found
7. Move the truncated files to their respective directories.
8. Optionally, create a log file of all operations

Is this something that Perl could be persuaded to do?

Yes.

In fact, given such a task, Perl would be my first choice. (Perl's not
the only language I use...)

BTW instead of copying the first N lines of a file and then overwriting
the original file with the truncated one, you can just as well use the
truncate() keyword... you may have to use seek() for this to take
effect.

Or you can use the -i command line switch, and stop after reading and
printing 25 lines.

To move a file, use the File::Copy module.
 
S

siva chelliah

I am amazed when people ask:
a) I have no clue about perl
b) I want to do X
c) Can perl do this?

It basically says that they want us to do their job. I am unemployed,
can I get that job (since I know how to do that!)?

Is there any recommended things to do to get a feel of a new language?
Such as:
a) write 'hello world'
b) Learn about string manipulations
c) File IO
d) and so on.


Siva
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,257
Messages
2,571,031
Members
48,768
Latest member
first4landlord

Latest Threads

Top