Ask a Question related to PERL Miscellaneous, Design and Development.
-
CountryLover #1
Can Perl do this?
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!
CountryLover Guest
-
Off Topic: Active Perl Native Windows / cygwin perl
I have both activestate windows native perl installed and the default cygwin perl. How can I have the cygwin shell use the windows perl rather... -
Control a non-perl image viewer from perl script
Below is a (non-finished) script that trys to run a linux viewer called eog (eye of gnome) in a script that will eventually allow me to power thru... -
Re : Installing CPAN Perl Modules with Activestate Perl 5. v5.8
Hi, In the process of trying to get perl modules installed, I downloaded over 300 Activestate specific perl modules and they work fine (of the ones... -
Effeciency question - perl scripts v's perl exe's
Max Adams wrote: The only existing Perl compiler is part of perl. Anything else is just a packager that puts a perl, required modules, and the... -
Designing Interfaces with Perl and Perl APIs
Hi, I am a long date perl programmer, and from time to time, I am having the same trouble: - To design interfaces for a self contained... -
Vlad Tepes #2
Re: Can Perl do this?
CountryLover <crazygonuts@pcgeek.invalid> wrote:
my @files = </path/to/dir/*>;>
> 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
FILE: foreach my $filename ( @files ) {> 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
open $fh, $filename or do {
warn "Can't open $filename: $!";
next FILE;
};
while ( <$fh> ) {
}
> 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!
>
>
--
Vlad
Vlad Tepes Guest
-
John Bokma #3
Re: Can Perl do this?
CountryLover wrote:
Parse as "traverse"? Sure. Hmmm, with just one directory it means> 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
getting all text files.
Yes, quite easy. I do similar things often with Perl :-)> 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?
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 ;-)> Any suggestions or code snippets would be much appreciated!
--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: [url]http://johnbokma.com/[/url] ICQ: 218175426
John web site hints: [url]http://johnbokma.com/websitedesign/[/url]
John Bokma Guest
-
Vlad Tepes #4
Re: Can Perl do this?
Vlad Tepes <minceme@start.no> wrote:
(> CountryLover <crazygonuts@pcgeek.invalid> wrote:
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. :-(
)
--
Vlad [ Zzzz...... ]
Vlad Tepes Guest
-
CountryLover #5
Re: Can Perl do this?
On Mon, 8 Sep 2003 01:30:01 +0000 (UTC), Vlad Tepes <minceme@start.no> wrote:
Thanks for the reply, Vlad, I really appreciate it. I've been doing alot of>CountryLover <crazygonuts@pcgeek.invalid> wrote:
>>>>
>> 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 = </path/to/dir/*>;
>>>> 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> ) {
>
>}
>
>
>>> 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!
>>
>>
this manually for several weeks, and it's gettin old fast :)
I'll try out your suggestions tonite.
Thanks again!
CountryLover Guest
-
James E Keenan #6
Re: Can Perl do this?
"CountryLover" <crazygonuts@pcgeek.invalid> wrote in message
news:7ilnlv48qm5raprrfmi7e1gfd1tvbn7guc@4ax.com...that I>
> First off, I know next to nothing about Perl, other than a few scriptsfilename> 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 sameYes.> 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?
Buy a good introductory Perl textbook such as Randal Schwartz's "Learning>
> Any suggestions or code snippets would be much appreciated!
>
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.
James E Keenan Guest
-
CountryLover #7
Re: Can Perl do this?
On Mon, 08 Sep 2003 03:33:15 +0200, John Bokma <postmaster@castleamber.com>
wrote:
Would a "pretty please" be sufficient to persuade you? :)>CountryLover wrote:
>>>> 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
>Parse as "traverse"? Sure. Hmmm, with just one directory it means
>getting all text files.
>>>> 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, 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.
>>>> Any suggestions or code snippets would be much appreciated!
>:-) I could be persuaded to write it for 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?!
CountryLover Guest
-
CountryLover #8
Re: Can Perl do this?
On Mon, 8 Sep 2003 01:38:39 +0000 (UTC), Vlad Tepes <minceme@start.no> wrote:
No prob, Vlad. I appreciate your effort nonetheless.>Vlad Tepes <minceme@start.no> wrote:>>> CountryLover <crazygonuts@pcgeek.invalid> wrote:
>(
> 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. :-(
>)
Have a good nite!
CountryLover Guest
-
John Bokma #9
Re: Can Perl do this?
CountryLover wrote:
An O'Reilly book would :-)> Would a "pretty please" be sufficient to persuade you? :)
Two books would make me write it now or tomorrow :-D> 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?!
--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: [url]http://johnbokma.com/[/url] ICQ: 218175426
John web site hints: [url]http://johnbokma.com/websitedesign/[/url]
John Bokma Guest
-
Jeff 'japhy' Pinyan #10
Re: Can Perl do this?
On Mon, 8 Sep 2003, CountryLover wrote:
That can be done in Perl without a temporary file. Roughly:>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
open FILE, "<+ $filename" or
die "can't open $filename for read/write: $!";
<FILE> for 1 .. 25;
truncate FILE, tell FILE;
seek FILE, 0, 0;>5. Parse the truncated files and find a specific keyword
while (<FILE>) {
# look for keywords
}
That could even be done in the previous chunk of code, stopping at the
25th line and truncating.
Do you mean that you want a copy of each file in each directory that the>6. Create directories based on the value of the keywords found
>7. Move the truncated files to their respective directories.
file has the keyword of? So that foo.txt, with keywords 'yucca', 'root',
and 'beer' would be in the yucca/, root/, and beer/ directories?
Simple.>8. Optionally, create a log file of all operations
--
Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
Jeff 'japhy' Pinyan Guest
-
Mike Flannigan #11
Re: Can Perl do this?
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
CountryLover wrote:
> 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?!Mike Flannigan Guest
-
John Bokma #12
Re: Can Perl do this?
Mike Flannigan wrote:
With "Learning Perl" and the "Cook book" I guess it could be done within> 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.
a week or less just by copy & paste and trail & error.
However Learning the basics of Perl takes months and months if not more.
I use Perl for almost 10 years and I learn quite often new things.
Worse, when I see scripts that are a few years old I sometimes wonder
how I ever came up with that solution.
--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: [url]http://johnbokma.com/[/url] ICQ: 218175426
John web site hints: [url]http://johnbokma.com/websitedesign/[/url]
John Bokma Guest
-
CountryLover #13
Re: Can Perl do this?
On Mon, 08 Sep 2003 04:07:20 +0200, John Bokma <postmaster@castleamber.com>
wrote:
Thanks, but if I'm gonna buy a book or two, I'll just use `em myself. :)>CountryLover wrote:
>>>> Would a "pretty please" be sufficient to persuade you? :)
>An O'Reilly book would :-)
>>>> 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?!
>Two books would make me write it now or tomorrow :-D
CountryLover Guest
-
CountryLover #14
Re: Can Perl do this?
On Mon, 08 Sep 2003 02:32:37 GMT, Mike Flannigan <mikeflan@earthlink.net>
wrote:
Agreed. However, I just don't have alot of time to devote to it right now on>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.
top of the stuff they pay me to do. :)
I'm going to grab a few books and see what I can whip up....
CountryLover Guest
-
John Bokma #15
Re: Can Perl do this?
CountryLover wrote:
:-D. Which I surely recommend. There are two kind of people: the book>>>Two books would make me write it now or tomorrow :-D
> Thanks, but if I'm gonna buy a book or two, I'll just use `em myself. :)
readers and people who hire book readers.
I can recommend the Perl Cook Book and make sure you get the latest
edition. To learn Perl either pick learning Perl or Programming Perl.
The latter expects quite some programming experience and is too hard for
most people to learn Perl I guess.
And where can you find a program, well documented and tested within a
few hours for just two O'Reilly books :-D.
--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: [url]http://johnbokma.com/[/url] ICQ: 218175426
John web site hints: [url]http://johnbokma.com/websitedesign/[/url]
John Bokma Guest
-
John W. Krahn #16
Re: Can Perl do this?
CountryLover wrote:
>
> 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
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Helgi Briem #17
Re: Can Perl do this?
On Mon, 08 Sep 2003 01:23:09 GMT, CountryLover
<crazygonuts@pcgeek.invalid> wrote:
perldoc perlrun>A project that I am working on requires the following steps:
>
>1. Parse a directory containing text files, potentially thousands
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)
perldoc -f open>2. Open each text file and read in the first 25 lines or so
perldoc perlvar ( $. )
perldoc perlsyn (while)
perldoc -f open>3. Write these lines to a temporary file
perldoc -f print
perldoc -f close
perldoc -f unlink>4. Delete each original file & replace with the temp file, using same filename
perldoc perlre (or perldoc perlrequick)>5. Parse the truncated files and find a specific keyword
perldoc -f mkdir>6. Create directories based on the value of the keywords found
perldoc -f rename>7. Move the truncated files to their respective directories.
perldoc -f open>8. Optionally, create a log file of all operations
perldoc -f print
Of course. What have you tried so far?>Is this something that Perl could be persuaded to do?
Helgi Briem Guest
-
Tim Hammerquist #18
Re: Can Perl do this?
John Bokma graced us by uttering:
<snip>> CountryLover wrote:Yes, it's possible with standard Unix tools. But as it will most>> > Is this something that Perl could be persuaded to do?
> 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.
likely be extended and expanded, I'd recommend Perl anyway. :)
Tim Hammerquist
--
Every man is guilty of all the good he didn't do.
-- Voltaire
Tim Hammerquist Guest
-
John Bokma #19
Re: Can Perl do this?
Tim Hammerquist wrote:
Indeed. I use Perl to forgot all the horrible experiences with sed, awk> John Bokma graced us by uttering:
>>>>CountryLover wrote:
> <snip>
>>>>>>>Is this something that Perl could be persuaded to do?
>>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.
> Yes, it's possible with standard Unix tools. But as it will most
> likely be extended and expanded, I'd recommend Perl anyway. :)
and friends :-)
--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: [url]http://johnbokma.com/[/url] ICQ: 218175426
John web site hints: [url]http://johnbokma.com/websitedesign/[/url]
John Bokma Guest
-
Bart Lateur #20
Re: Can Perl do this?
CountryLover wrote:
Yes.>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?
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.
--
Bart.
Bart Lateur Guest



Reply With Quote

