[23237] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 5458 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Sep 8 00:05:57 2003

Date: Sun, 7 Sep 2003 21:05:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 7 Sep 2003     Volume: 10 Number: 5458

Today's topics:
        Can Perl do this?  <crazygonuts@pcgeek.invalid>
    Re: Can Perl do this? <minceme@start.no>
    Re: Can Perl do this? <postmaster@castleamber.com>
    Re: Can Perl do this? <minceme@start.no>
    Re: Can Perl do this? <crazygonuts@pcgeek.invalid>
    Re: Can Perl do this? <jkeen@concentric.net>
    Re: Can Perl do this? <crazygonuts@pcgeek.invalid>
    Re: Can Perl do this? <crazygonuts@pcgeek.invalid>
    Re: Can Perl do this? <postmaster@castleamber.com>
    Re: Can Perl do this? <pinyaj@rpi.edu>
    Re: Can Perl do this? <mikeflan@earthlink.net>
    Re: Can Perl do this? <postmaster@castleamber.com>
    Re: Can Perl do this? <crazygonuts@pcgeek.invalid>
    Re: Can Perl do this? <crazygonuts@pcgeek.invalid>
    Re: Can Perl do this? <postmaster@castleamber.com>
    Re: Can Perl do this? <krahnj@acm.org>
    Re: Help configuring Perl with Apache 2 <flavell@mail.cern.ch>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Mon, 08 Sep 2003 01:23:09 GMT
From: CountryLover <crazygonuts@pcgeek.invalid>
Subject: Can Perl do this? 
Message-Id: <7ilnlv48qm5raprrfmi7e1gfd1tvbn7guc@4ax.com>


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!




------------------------------

Date: Mon, 8 Sep 2003 01:30:01 +0000 (UTC)
From: Vlad Tepes <minceme@start.no>
Subject: Re: Can Perl do this?
Message-Id: <bjgm2p$qbm$1@troll.powertech.no>

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!
>
>


-- 
Vlad


------------------------------

Date: Mon, 08 Sep 2003 03:33:15 +0200
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Can Perl do this?
Message-Id: <1062984899.637052@halkan.kabelfoon.nl>

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 ;-)

-- 
Kind regards,       feel free to mail: mail(at)johnbokma.com (or reply)
                     virtual home: http://johnbokma.com/  ICQ: 218175426
John                web site hints: http://johnbokma.com/websitedesign/



------------------------------

Date: Mon, 8 Sep 2003 01:38:39 +0000 (UTC)
From: Vlad Tepes <minceme@start.no>
Subject: Re: Can Perl do this?
Message-Id: <bjgmiu$qgv$1@troll.powertech.no>

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...... ]


------------------------------

Date: Mon, 08 Sep 2003 01:45:49 GMT
From: CountryLover <crazygonuts@pcgeek.invalid>
Subject: Re: Can Perl do this?
Message-Id: <1onnlvg6s8jpsh57p688pjnulpdsn70hmj@4ax.com>

On Mon, 8 Sep 2003 01:30:01 +0000 (UTC), Vlad Tepes <minceme@start.no> wrote:

>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!
>>
>>

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!



------------------------------

Date: 08 Sep 2003 01:47:19 GMT
From: "James E Keenan" <jkeen@concentric.net>
Subject: Re: Can Perl do this?
Message-Id: <bjgn37$rlc@dispatch.concentric.net>


"CountryLover" <crazygonuts@pcgeek.invalid> wrote in message
news:7ilnlv48qm5raprrfmi7e1gfd1tvbn7guc@4ax.com...
>
> 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.




------------------------------

Date: Mon, 08 Sep 2003 01:48:04 GMT
From: CountryLover <crazygonuts@pcgeek.invalid>
Subject: Re: Can Perl do this?
Message-Id: <kqnnlv8e4k2j3hr2iqvkisspceitjepm4s@4ax.com>

On Mon, 08 Sep 2003 03:33:15 +0200, John Bokma <postmaster@castleamber.com>
wrote:

>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 ;-)

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?!



------------------------------

Date: Mon, 08 Sep 2003 01:48:49 GMT
From: CountryLover <crazygonuts@pcgeek.invalid>
Subject: Re: Can Perl do this?
Message-Id: <ounnlvgjb7mdq2ujnnm8c89jmnhpfukth8@4ax.com>

On Mon, 8 Sep 2003 01:38:39 +0000 (UTC), Vlad Tepes <minceme@start.no> wrote:

>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.  :-( 
>)

No prob, Vlad. I appreciate your effort nonetheless.

Have a good nite!



------------------------------

Date: Mon, 08 Sep 2003 04:07:20 +0200
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Can Perl do this?
Message-Id: <1062986944.942561@halkan.kabelfoon.nl>

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

-- 
Kind regards,       feel free to mail: mail(at)johnbokma.com (or reply)
                     virtual home: http://johnbokma.com/  ICQ: 218175426
John                web site hints: http://johnbokma.com/websitedesign/



------------------------------

Date: Sun, 7 Sep 2003 22:15:47 -0400
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
Subject: Re: Can Perl do this?
Message-Id: <Pine.SGI.3.96.1030907221148.6646C-100000@vcmr-64.server.rpi.edu>

On Mon, 8 Sep 2003, CountryLover wrote:

>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
    die "can't open $filename for read/write: $!";
  <FILE> for 1 .. 25;
  truncate FILE, tell FILE;

>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.

-- 
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)



------------------------------

Date: Mon, 08 Sep 2003 02:32:37 GMT
From: Mike Flannigan <mikeflan@earthlink.net>
Subject: Re: Can Perl do this?
Message-Id: <3F5BEAE1.C043A59A@earthlink.net>


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?!



------------------------------

Date: Mon, 08 Sep 2003 04:34:34 +0200
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Can Perl do this?
Message-Id: <1062988578.890668@halkan.kabelfoon.nl>

Mike Flannigan wrote:

> 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.

With "Learning Perl" and the "Cook book" I guess it could be done within 
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: http://johnbokma.com/  ICQ: 218175426
John                web site hints: http://johnbokma.com/websitedesign/



------------------------------

Date: Mon, 08 Sep 2003 02:39:46 GMT
From: CountryLover <crazygonuts@pcgeek.invalid>
Subject: Re: Can Perl do this?
Message-Id: <8uqnlv0csvnlq4sgh18rqvlq0sak5k4mo6@4ax.com>

On Mon, 08 Sep 2003 04:07:20 +0200, John Bokma <postmaster@castleamber.com>
wrote:

>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

Thanks, but if I'm gonna buy a book or two, I'll just use `em myself.  :)



------------------------------

Date: Mon, 08 Sep 2003 02:44:56 GMT
From: CountryLover <crazygonuts@pcgeek.invalid>
Subject: Re: Can Perl do this?
Message-Id: <o4rnlv8s9hkaf47bjaun0p83c18bg189f1@4ax.com>

On Mon, 08 Sep 2003 02:32:37 GMT, Mike Flannigan <mikeflan@earthlink.net>
wrote:

>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....
 


------------------------------

Date: Mon, 08 Sep 2003 04:45:22 +0200
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Can Perl do this?
Message-Id: <1062989226.586105@halkan.kabelfoon.nl>

CountryLover wrote:

>>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.  :)

:-D. Which I surely recommend. There are two kind of people: the book 
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: http://johnbokma.com/  ICQ: 218175426
John                web site hints: http://johnbokma.com/websitedesign/



------------------------------

Date: Mon, 08 Sep 2003 03:43:50 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Can Perl do this?
Message-Id: <3F5BFAFF.43C3E1A@acm.org>

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


------------------------------

Date: Mon, 8 Sep 2003 02:52:37 +0200
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: Help configuring Perl with Apache 2
Message-Id: <Pine.LNX.4.53.0309080242480.8401@lxplus007.cern.ch>

On Sun, Sep 7, Cyde Weys inscribed on the eternal scroll:

> Vlad Tepes wrote:
>
> > Since you're now plonked by Tad and Abigail, you're not likely to get an
> > answer from them.
>
> Yeah, I figure as much.  I'm not stupid.  But sometimes people only say
> *plonk* just to show off or make a statement, but they never actually go
> through with their threat.

When you find yourself down a hole, it's wise to stop digging.  If you
didn't know beforehand that Tad and Abigail have their respective
reputations on this group, it's not hard to find out.

> I should have phrased my question more carefully then.  I didn't mean,
> "I know this belongs in another group but I'll post it here just to
> screw with you guys and lower the s/n ratio."  I meant, "Perhaps this is
> more applicable to an Apache newsgroup, but since the question deals
> with how to configure Apache to work WITH PERL, I think I might get more
> knowledgable responses here."

The knowledgeable responses that you got seemed to show that you have
two evident problems: one of them belongs on the c.i.w.servers.*
hierarchy as it relates to Apache configuration, and the other belongs
on the c.i.w.authoring.cgi group as it relates specifically to the
CGI.  At no point did you exhibit anything that was a Perl problem
(you clearly have the ability to write a syntactically correct print
statement in Perl, just that what you printed wasn't useful for its
purpose).  I initially gave you the benefit of the doubt, where others
evidently felt you had already gone too far with the discourteous way
you expressed your question.

You might not have known that before, but you better believe it now,
if you're to stand any hope of convincing the hon. Usenauts of your
claim that you're 'not stupid'.

best regards


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 5458
***************************************


home help back first fref pref prev next nref lref last post