[24651] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6815 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Aug 3 13:47:11 2004

Date: Tue, 3 Aug 2004 10:46:11 -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           Tue, 3 Aug 2004     Volume: 10 Number: 6815

Today's topics:
    Re: NEWBIE: Perls System -command and Cygwin bash-shell <pekka.niiranen@wlanmail.com>
    Re: NEWBIE: Perls System -command and Cygwin bash-shell (Anno Siegel)
    Re: NEWBIE: Perls System -command and Cygwin bash-shell <spamtrap@dot-app.org>
    Re: NEWBIE: Perls System -command and Cygwin bash-shell <Joe.Smith@inwap.com>
    Re: NEWBIE: Perls System -command and Cygwin bash-shell (Anno Siegel)
    Re: NEWBIE: Perls System -command and Cygwin bash-shell <1usa@llenroc.ude.invalid>
        not recognizing a new module <bsinha@qualcomm.com>
    Re: not recognizing a new module <mritty@gmail.com>
    Re: Over Interpolation of Variables in a Substitution <Joe.Smith@inwap.com>
    Re: Over Interpolation of Variables in a Substitution <usenet@morrow.me.uk>
        Parse log files for data <agent@thwackspam.fathom.org>
    Re: Parse log files for data <gogala@sbcglobal.net>
    Re: Parse log files for data <tadmc@augustmail.com>
    Re: Parse log files for data <agent@thwackspam.fathom.org>
    Re: Parse log files for data <agent@thwackspam.fathom.org>
    Re: Parse log files for data <bowsayge@nomail.afraid.org>
    Re: Parse log files for data <gogala@sbcglobal.net>
    Re: Parse log files for data <Joe.Smith@inwap.com>
    Re: Parse log files for data <agent@thwackspam.fathom.org>
    Re: Parse log files for data <agent@thwackspam.fathom.org>
    Re: Parse log files for data <jgibson@mail.arc.nasa.gov>
    Re: Parse log files for data (Anno Siegel)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 23 Jul 2004 09:25:49 GMT
From: pekka niiranen <pekka.niiranen@wlanmail.com>
Subject: Re: NEWBIE: Perls System -command and Cygwin bash-shell, More details
Message-Id: <xQ4Mc.89$yH4.42@read3.inet.fi>

Ok here goes,

---the perl script named "p" starts--
#!/usr/local/bin/perl -w
unless (opendir(TMPDIR, "/cygdrive/c/home/cygwin/tmp")) {
	print "Can't open temporary directory !";
}
system("/cygdrive/c/home/cygwin/s");	# run the shell script
while( defined ($tmpfile = readdir TMPDIR) ) {
	next if $tmpfile =~ /^\.\.?$/;
	system("chmod 660 $tmpfile") or die;	
}
system("ls -l ./tmp");
closedir(TMPDIR);

---the perl script named "p" stops---

---the bash script named "s" starts---

#!/usr/bin/bash
echo "Blah Blah" > ./tmp/tfile
exit
---the bash script named "s" stops---

The perl script and shell script are run from the same directory.
The file "tfile" is created into subdirectory "tmp"

"ls -l" from script directory gives (among other things):
drwxr-xr-x+   2 treniira Administ        0 Jul 23 12:11 tmp/

Perl scripts output is:
[vat58008:~] $ ./p
chmod: getting attributes of `tfile': No such file or directory
total 1
-rw-r--r--    1 treniira Administ       10 Jul 23 12:23 tfile


-pekka-

Anno Siegel wrote:
> Pekka Niiranen  <pekka.niiranen@wlanmail.com> wrote in comp.lang.perl.misc:
> 
>>Hi there,
>>
>>I am having problem in W2K when using Cygwin's Perl.
>>My Perl script starts Shell script with System -command; 
>>system(scriptfile). The shell script creates a temporary file
>>like this:
>>
>>#!/usr/bin/sh
>>cat "blah blah" > tempfile
>>
>>and then exits back to Perl. When I then try to remove
>>the created "tempfile" from the same Perl script with
>>"unlink" I find out that Perl does not have access rights to the file.
> 
> 
> So what *are* the permissions and ownership of the file?  What
> does "ls -l" say?  You are not giving us vital information.
> 
> 
>>If I use "readdir" Perl finds the file but nor command
>>"system(rm $file)" or "unlink($file)" does not work either. However, 
> 
> 
> Does $file contain what you think it does?
> 
> 
>>when Perl script exits I can remove the file with
>>normal Bash shell command: "rm tempfile".
>>
>>It seems that Perl is running with different user rights
>>than the Shell script. My question is therefore: "How can I remove file 
>>created by the Bash shell script started as subshell from Perl script?"
> 
> 
> No idea.  Show complete code that demonstrates the problem.
> 
> Anno


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

Date: 23 Jul 2004 10:45:44 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: NEWBIE: Perls System -command and Cygwin bash-shell, More details
Message-Id: <cdqq8o$1ot$1@mamenchi.zrz.TU-Berlin.DE>

Please don't top-post.  I moved your reply in context.

pekka niiranen  <pekka.niiranen@wlanmail.com> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > Pekka Niiranen  <pekka.niiranen@wlanmail.com> wrote in comp.lang.perl.misc:
> > 
> >>Hi there,
> >>
> >>I am having problem in W2K when using Cygwin's Perl.
> >>My Perl script starts Shell script with System -command; 
> >>system(scriptfile). The shell script creates a temporary file
> >>like this:
> >>
> >>#!/usr/bin/sh
> >>cat "blah blah" > tempfile
> >>
> >>and then exits back to Perl. When I then try to remove
> >>the created "tempfile" from the same Perl script with
> >>"unlink" I find out that Perl does not have access rights to the file.
> > 
> > 
> > So what *are* the permissions and ownership of the file?  What
> > does "ls -l" say?  You are not giving us vital information.
> > 
> > 
> >>If I use "readdir" Perl finds the file but nor command
> >>"system(rm $file)" or "unlink($file)" does not work either. However, 
> > 
> > 
> > Does $file contain what you think it does?
> > 
> > 
> >>when Perl script exits I can remove the file with
> >>normal Bash shell command: "rm tempfile".
> >>
> >>It seems that Perl is running with different user rights
> >>than the Shell script. My question is therefore: "How can I remove file 
> >>created by the Bash shell script started as subshell from Perl script?"
> > 
> > 
> > No idea.  Show complete code that demonstrates the problem.
> > 
> > Anno


> Ok here goes,
> 
> ---the perl script named "p" starts--
> #!/usr/local/bin/perl -w
> unless (opendir(TMPDIR, "/cygdrive/c/home/cygwin/tmp")) {
> 	print "Can't open temporary directory !";
> }

Checking "opendir" for errors is good.

> system("/cygdrive/c/home/cygwin/s");	# run the shell script

Not checking "system" for errors is not so good.

> while( defined ($tmpfile = readdir TMPDIR) ) {
> 	next if $tmpfile =~ /^\.\.?$/;
> 	system("chmod 660 $tmpfile") or die;	

That won't work, unless your current directory happens to be
/cygdrive/c/home/cygwin/tmp, where the file is.  That's the
reason for your error.  Instead use

    chmod 0660, "/cygdrive/c/home/cygwin/tmp/$tmpfile" or die "chmod";

Note that I replaced the system call with Perl's internal chmod.

> }
> system("ls -l ./tmp");
> closedir(TMPDIR);

[snip]

Anno


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

Date: Fri, 23 Jul 2004 07:09:33 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: NEWBIE: Perls System -command and Cygwin bash-shell, More details
Message-Id: <SfSdnWlJmstwbJ3cRVn-pQ@adelphia.com>

Anno Siegel wrote:
> 
> pekka niiranen  <pekka.niiranen@wlanmail.com> wrote in comp.lang.perl.misc:
> 
>>	print "Can't open temporary directory !";
> 
> Checking "opendir" for errors is good.

Yes, and reporting the cause of those errors is even better:

     print "Can't open temporary dir: $!";

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


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

Date: Sun, 25 Jul 2004 09:47:21 GMT
From: Joe Smith <Joe.Smith@inwap.com>
Subject: Re: NEWBIE: Perls System -command and Cygwin bash-shell, More details
Message-Id: <JkLMc.25910$8_6.13379@attbi_s04>

pekka niiranen wrote:

> unless (opendir(TMPDIR, "/cygdrive/c/home/cygwin/tmp")) {
>     print "Can't open temporary directory !";
> }
> system("/cygdrive/c/home/cygwin/s");    # run the shell script
> while( defined ($tmpfile = readdir TMPDIR) ) {

It is not a good idea to opendir a directory before creating a new
file in that directory.  The readdir() may or may not see the new file.

Any time you opendir a directory other than '.', you must remember
to prepend the directory path to whatever readdir() returns.

   system("/home/cygwin/s");	# Use the cygwin name not windows name
   $dir = "/home/cygwin/tmp";	# Use the cygwin name not windows name
   open(TMPDIR,$dir) or die "Cannot open directory $dir: $!";
   while (defined ($dir_entry = readdir TMPDIR) ) {
     $tmpfile = "$dir/$dir_entry";
	...
    }

	-Joe


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

Date: 23 Jul 2004 08:25:28 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: NEWBIE: Perls System -command and Cygwin bash-shell
Message-Id: <cdqi1o$puq$1@mamenchi.zrz.TU-Berlin.DE>

Pekka Niiranen  <pekka.niiranen@wlanmail.com> wrote in comp.lang.perl.misc:
> Hi there,
> 
> I am having problem in W2K when using Cygwin's Perl.
> My Perl script starts Shell script with System -command; 
> system(scriptfile). The shell script creates a temporary file
> like this:
> 
> #!/usr/bin/sh
> cat "blah blah" > tempfile
> 
> and then exits back to Perl. When I then try to remove
> the created "tempfile" from the same Perl script with
> "unlink" I find out that Perl does not have access rights to the file.

So what *are* the permissions and ownership of the file?  What
does "ls -l" say?  You are not giving us vital information.

> If I use "readdir" Perl finds the file but nor command
> "system(rm $file)" or "unlink($file)" does not work either. However, 

Does $file contain what you think it does?

> when Perl script exits I can remove the file with
> normal Bash shell command: "rm tempfile".
> 
> It seems that Perl is running with different user rights
> than the Shell script. My question is therefore: "How can I remove file 
> created by the Bash shell script started as subshell from Perl script?"

No idea.  Show complete code that demonstrates the problem.

Anno


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

Date: 23 Jul 2004 09:14:40 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: NEWBIE: Perls System -command and Cygwin bash-shell
Message-Id: <Xns952F3558AE621asu1cornelledu@132.236.56.8>

Pekka Niiranen <pekka.niiranen@wlanmail.com> wrote in news:4100130f$0
$13046$39db0f71@news.song.fi:

> and then exits back to Perl. When I then try to remove
> the created "tempfile" from the same Perl script with
> "unlink" I find out that Perl does not have access rights to the file.
> If I use "readdir" Perl finds the file but nor command
> "system(rm $file)" or "unlink($file)" does not work either. However, 
> when Perl script exits I can remove the file with
> normal Bash shell command: "rm tempfile".
> 
> It seems that Perl is running with different user rights

I find that highly unlikely.

> than the Shell script. My question is therefore: "How can I remove file 
> created by the Bash shell script started as subshell from Perl script?"
> Should I start another Shell script that executes "rm tempfile"
> just for that task?

In addition to Anno's suggestions, I have one question. Do you read from 
the tempfile in your Perl script. If you do, does your script close the 
file handle before attempting to call unlink? 

-- 
A. Sinan Unur
1usa@llenroc.ude.invalid 
(remove '.invalid' and reverse each component for email address)



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

Date: Thu, 29 Jul 2004 13:30:21 -0700
From: "Bharat Sinha" <bsinha@qualcomm.com>
Subject: not recognizing a new module
Message-Id: <cebmoh$njd$1@fair.qualcomm.com>

Hi,
When compiling, I got the following error:
Can't locate loadable object for module Term::ReadKey in @INC (@INC
contains: C:Program Files/Perl/lib /C:/Program Files/Perl/site/lib...)

I found that the 'C:Program Files/Perl/lib/term' folder was missing the
readkey module, so I got it from cpan and out it in the 'term' folder. But I
still get the same error.

Do I need to somehow update something in perl to make it consider any new
additions.
Thanks




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

Date: Thu, 29 Jul 2004 16:45:43 -0400
From: Paul Lalli <mritty@gmail.com>
Subject: Re: not recognizing a new module
Message-Id: <20040729164248.W3404@barbara.cs.rpi.edu>

On Thu, 29 Jul 2004, Bharat Sinha wrote:

> Hi,
> When compiling, I got the following error:
> Can't locate loadable object for module Term::ReadKey in @INC (@INC
> contains: C:Program Files/Perl/lib /C:/Program Files/Perl/site/lib...)
>
> I found that the 'C:Program Files/Perl/lib/term' folder was missing the
> readkey module, so I got it from cpan and out it in the 'term' folder. But I
> still get the same error.
>
> Do I need to somehow update something in perl to make it consider any new
> additions.
> Thanks

When you say you "got it from cpan", do you mean you just copied the .pm
file and put it in that directory?  You can't do that.  Term::Readkey is
not a pure perl module.  It has an executable portion.  You need to follow
the standard installation instructions:  Grab the entire distribution from
CPAN, unzip and untar it, run perl Makefile.pl, run make, run make test,
run make install.

Now, if you use the CPAN module, this is a lot easier:

perl -MCPAN -e'install Term::Readkey'

Paul Lalli


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

Date: Sun, 25 Jul 2004 09:50:47 GMT
From: Joe Smith <Joe.Smith@inwap.com>
Subject: Re: Over Interpolation of Variables in a Substitution
Message-Id: <XnLMc.155320$a24.133670@attbi_s03>

Dietrich wrote:

> Challenge:
> 	I have variables in a configuration file that read ${profile} or
> ${altdatabase}, etc.  They are also the pattern that I am using for a
> substitution, but the challenge is that they get interpolated more
> (resolved/dereferenced) than I would like.

In that case, you need to use a backslash before the dollarsign.

    s/\$altdatabase/$altdatabase/g;
    $/\${profile}/${profile}/g;

	-Joe


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

Date: Fri, 23 Jul 2004 00:22:50 +0100
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Over Interpolation of Variables in a Substitution
Message-Id: <aru5t1-vl5.ln1@mauzo.dyndns.org>


Quoth dietrichm@mac.com (Dietrich):
> Challenge:
> 	I have variables in a configuration file that read ${profile} or
> ${altdatabase}, etc.  They are also the pattern that I am using for a
> substitution, but the challenge is that they get interpolated more
> (resolved/dereferenced) than I would like.  I have found a way around
> them, but that means removing the use strict pragma and -w option in
> the magic cookie ( top line of of a shell or perl script).
> 
> I initially had substitution problems.  The pattern on the left was
> never found, as it resolved what $string_match would be and that was
> not a found regexp.  This is what I mean by the title, Over
> Interpolation of Variables in a Substitution.
> 
> My method to get the script to work is to add an extra $ to the
> substitution code as follows, in order to prevent the over
> interpolation:
> 
>     29          s/$$string_match/$resolved_value/;

AAARGH!!! 'You can't just make shit up and expect the computer to
understand' [mjd].

Read the docs. The correct way to write what you want is

s/\Q$string_match/$resolved_value/;

 . What you have there is a symref: read perldoc perlref to understand
what they are, why you just used one and why you shouldn't. In this
case, the most straightforward reason why you shouldn't is because it
does something completely different from what you thought it did: if
$string_match = 'foo' this will look for a variable $foo and substitute
*that* into the rexex.

Ben

-- 
  The cosmos, at best, is like a rubbish heap scattered at random.
                                                         - Heraclitus
  ben@morrow.me.uk


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

Date: Sat, 31 Jul 2004 05:49:11 GMT
From: Dan <agent@thwackspam.fathom.org>
Subject: Parse log files for data
Message-Id: <ldbmg01906iesghi9ah0mmod5n5qnd0naa@4ax.com>

I have a bunch of custom log files with database queries in them, and
would like to extract all the insert and update queries so I can
recreate some lost data.

I'm pretty sure this is the perfect exercise for a perl script.  I
just don't have the vocabulary to write it myself.  But this is all it
needs to do:

* Open, in order, all log files named yyyymmdd.log in the current
directory.  (where yyyymmdd is what you think it is)

* For each file, look at each line, and if it begins with " query=
INSERT" or " query = UPDATE", write that entire line starting from the
word "INSERT" or "UPDATE" to the output file.

I'm not sure what version of perl I have or if that matters, but my
system is Fedora Core 2, so whatever comes with that...

Any takers?  The 2 minutes of your time would be greatly appreciated.
:)


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

Date: Sat, 31 Jul 2004 02:51:40 -0400
From: Mladen Gogala <gogala@sbcglobal.net>
Subject: Re: Parse log files for data
Message-Id: <pan.2004.07.31.06.51.37.457786@sbcglobal.net>

On Sat, 31 Jul 2004 05:49:11 +0000, Dan wrote:

> I have a bunch of custom log files with database queries in them, and
> would like to extract all the insert and update queries so I can
> recreate some lost data.
> 
> I'm pretty sure this is the perfect exercise for a perl script.  I
> just don't have the vocabulary to write it myself.  But this is all it
> needs to do:
> 
> * Open, in order, all log files named yyyymmdd.log in the current
> directory.  (where yyyymmdd is what you think it is)
> 
> * For each file, look at each line, and if it begins with " query=
> INSERT" or " query = UPDATE", write that entire line starting from the
> word "INSERT" or "UPDATE" to the output file.


find mydir -name logfile -exec cat {} \;|perl -ne  'if (/\s+query =([UI].*)/) { print "$1\n"; }'


> 
> I'm not sure what version of perl I have or if that matters, but my
> system is Fedora Core 2, so whatever comes with that...
> 
> Any takers?  The 2 minutes of your time would be greatly appreciated.
> :)

-- 
A city is a large community where people are lonesome together.



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

Date: Sat, 31 Jul 2004 08:41:05 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Parse log files for data
Message-Id: <slrncgn8bh.22j.tadmc@magna.augustmail.com>

Dan <agent@thwackspam.fathom.org> wrote:
> I have a bunch of custom log files with database queries in them, and
> would like to extract all the insert and update queries so I can
> recreate some lost data.
> 

> * Open, in order, all log files named yyyymmdd.log in the current
> directory.  (where yyyymmdd is what you think it is)

   opendir DOT, '.' or die "could not open current directory  $!";
   foreach my $fname ( sort grep /^\d{8}\.log$/, readdir DIR ) {  # untested
      open FILE, $fname or die "could not open '$fname' $!";
   

> * For each file, look at each line, 


      while ( <FILE> ) {


> and if it begins with " query=
> INSERT" or " query = UPDATE", 

         if ( /^ query = (INSERT|UPDATE)/ ) {

> write that entire line starting from the
> word "INSERT" or "UPDATE" to the output file.


            print;

         }
      }
   }
   closedir DIR;


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Sat, 31 Jul 2004 13:58:09 GMT
From: Dan <agent@thwackspam.fathom.org>
Subject: Re: Parse log files for data
Message-Id: <7t8ng0pd1v6u8vmijjoo8ln37ls2evnt4q@4ax.com>

On Sat, 31 Jul 2004 02:51:40 -0400, Mladen Gogala
<gogala@sbcglobal.net> wrote:

>On Sat, 31 Jul 2004 05:49:11 +0000, Dan wrote:
>
>> * Open, in order, all log files named yyyymmdd.log in the current
>> directory.  (where yyyymmdd is what you think it is)
>> 
>> * For each file, look at each line, and if it begins with " query=
>> INSERT" or " query = UPDATE", write that entire line starting from the
>> word "INSERT" or "UPDATE" to the output file.
>
>
>find mydir -name logfile -exec cat {} \;|perl -ne  'if (/\s+query =([UI].*)/) { print "$1\n"; }'

Even easier than I thought!  But it didn't quite work...

I replaced mydir with . and logfile with *.log, but it says 'paths
must precede expression'.  I also tried ./ for mydir.  Same result.

Also, am I supposed to put anything into the empty {}?  I don't see
anything that specifies an output file.  And what is the \; for?


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

Date: Sat, 31 Jul 2004 15:17:43 GMT
From: Dan <agent@thwackspam.fathom.org>
Subject: Re: Parse log files for data
Message-Id: <fndng0pmr62nfq6h1824vpmaee9jilbl3u@4ax.com>

On Sat, 31 Jul 2004 13:58:09 GMT, Dan <agent@thwackspam.fathom.org>
wrote:

>On Sat, 31 Jul 2004 02:51:40 -0400, Mladen Gogala
><gogala@sbcglobal.net> wrote:
>
>>On Sat, 31 Jul 2004 05:49:11 +0000, Dan wrote:
>>
>>> * Open, in order, all log files named yyyymmdd.log in the current
>>> directory.  (where yyyymmdd is what you think it is)
>>> 
>>> * For each file, look at each line, and if it begins with " query=
>>> INSERT" or " query = UPDATE", write that entire line starting from the
>>> word "INSERT" or "UPDATE" to the output file.
>>
>>
>>find mydir -name logfile -exec cat {} \;|perl -ne  'if (/\s+query =([UI].*)/) { print "$1\n"; }'
>
>Even easier than I thought!  But it didn't quite work...
>
>I replaced mydir with . and logfile with *.log, but it says 'paths
>must precede expression'.  I also tried ./ for mydir.  Same result.
>
>Also, am I supposed to put anything into the empty {}?  I don't see
>anything that specifies an output file.  And what is the \; for?

Ok, I found I had to put *.log in "quotes", so that solved that...
but now it's simply not finding anything.  Do I need to do anything to
my system to enable regular expressions?

ls -1 *.log
gives me an alphabetical listing of my log files.

ls -1 *.log | grep /.*/
returns nothing.  No matter what I put into the regular expression,
whether here or in the if statement of the perl suggested above, it
finds nothing.

Do I need to take this to a general linux group?


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

Date: Sat, 31 Jul 2004 18:28:22 GMT
From: bowsayge <bowsayge@nomail.afraid.org>
Subject: Re: Parse log files for data
Message-Id: <axROc.4097$Jp6.857@newsread3.news.atl.earthlink.net>

Dan said to us:

> I have a bunch of custom log files with database queries in them, and
> would like to extract all the insert and update queries so I can
> recreate some lost data.
> 
> I'm pretty sure this is the perfect exercise for a perl script.  I
> just don't have the vocabulary to write it myself.  But this is all it
> needs to do:
> 
> * Open, in order, all log files named yyyymmdd.log in the current
> directory.  (where yyyymmdd is what you think it is)
> 
> * For each file, look at each line, and if it begins with " query=
> INSERT" or " query = UPDATE", write that entire line starting from the
> word "INSERT" or "UPDATE" to the output file.
> 
> I'm not sure what version of perl I have or if that matters, but my
> system is Fedora Core 2, so whatever comes with that...
> 
> Any takers?  The 2 minutes of your time would be greatly appreciated.
> :)

This wasn't tested because there was no suitable data:


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

main();

sub main {        
    local ($_, $");
    my (@files);
                           
    @files = <*.log>;      
    @files = grep /^[[:digit:]]{8}\.log$/, @files;
                           
    foreach my $file (@files) {
        my ($fh);          
        open ($fh, '<', $file) or die ("Error: $!");
        while (<$fh>) {    
            print if (/^query = (INSERT|UPDATE)/x);
        }        
        close $fh;
    }

    print "files: @files\n";
}


-- 
bowsayge



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

Date: Sat, 31 Jul 2004 14:49:19 -0400
From: Mladen Gogala <gogala@sbcglobal.net>
Subject: Re: Parse log files for data
Message-Id: <pan.2004.07.31.18.49.19.74937@sbcglobal.net>

On Sat, 31 Jul 2004 18:28:22 +0000, bowsayge wrote:

> while (<$fh>) {    
>             print if (/^query = (INSERT|UPDATE)/x);
>         }

That will print the whole line. He wants to print just the part after
INSERT or UPDATE.

-- 
A city is a large community where people are lonesome together.



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

Date: Sat, 31 Jul 2004 19:39:52 GMT
From: Joe Smith <Joe.Smith@inwap.com>
Subject: Re: Parse log files for data
Message-Id: <bASOc.59118$8_6.33445@attbi_s04>

Dan wrote:

> ls -1 *.log
> gives me an alphabetical listing of my log files.
> 
> ls -1 *.log | grep /.*/
> returns nothing.  No matter what I put into the regular expression,

   ls -1 *.log | grep '/.*/'


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

Date: Sun, 01 Aug 2004 05:32:43 GMT
From: Dan <agent@thwackspam.fathom.org>
Subject: Re: Parse log files for data
Message-Id: <k30pg0d4t5nmot066fk4bfh4g6fg4l8ia0@4ax.com>

On Sat, 31 Jul 2004 19:39:52 GMT, Joe Smith <Joe.Smith@inwap.com>
wrote:

>Dan wrote:
>
>> ls -1 *.log
>> gives me an alphabetical listing of my log files.
>> 
>> ls -1 *.log | grep /.*/
>> returns nothing.  No matter what I put into the regular expression,
>
>   ls -1 *.log | grep '/.*/'

returns nothing.


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

Date: Sun, 01 Aug 2004 06:02:13 GMT
From: Dan <agent@thwackspam.fathom.org>
Subject: Re: Parse log files for data
Message-Id: <ns0pg0lcclso81ebf0hreki89qvq0homi4@4ax.com>

On Sat, 31 Jul 2004 14:49:19 -0400, Mladen Gogala
<gogala@sbcglobal.net> wrote:

>On Sat, 31 Jul 2004 18:28:22 +0000, bowsayge wrote:
>
>> while (<$fh>) {    
>>             print if (/^query = (INSERT|UPDATE)/x);
>>         }
>
>That will print the whole line. He wants to print just the part after
>INSERT or UPDATE.

Not just after, but including.  If it grabs the whole line that's
fine.  I can easily remove the " query= " from the beginning of each
line.

But at the moment it's not grabbing anything.  It just lists all the
files at the end.

Here's a sample entry from one of the log files (just the beginning of
each line):

===00:23:54 on Mon July 26, 2004===
 HTTP_REFERER= 
 HTTP_HOST= www.vi...
 REQUEST_URI= /ult...
 HTTP_USER_AGENT= Moz...
 REMOTE_ADDR= 203...
 query= INSERT INTO sc...

Spacing is slightly different, so I modified the regex accordingly to
(/^ query= (INSERT|UPDATE)/x) but it still found nothing.


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

Date: Mon, 02 Aug 2004 12:54:02 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Parse log files for data
Message-Id: <020820041254028354%jgibson@mail.arc.nasa.gov>

In article <ns0pg0lcclso81ebf0hreki89qvq0homi4@4ax.com>, Dan
<agent@thwackspam.fathom.org> wrote:

> On Sat, 31 Jul 2004 14:49:19 -0400, Mladen Gogala
> <gogala@sbcglobal.net> wrote:
> 
> >On Sat, 31 Jul 2004 18:28:22 +0000, bowsayge wrote:
> >
> >> while (<$fh>) {    
> >>             print if (/^query = (INSERT|UPDATE)/x);
> >>         }
> >
> >That will print the whole line. He wants to print just the part after
> >INSERT or UPDATE.
> 
> Not just after, but including.  If it grabs the whole line that's
> fine.  I can easily remove the " query= " from the beginning of each
> line.
> 
> But at the moment it's not grabbing anything.  It just lists all the
> files at the end.
> 
> Here's a sample entry from one of the log files (just the beginning of
> each line):
> 
> ===00:23:54 on Mon July 26, 2004===
>  HTTP_REFERER= 
>  HTTP_HOST= www.vi...
>  REQUEST_URI= /ult...
>  HTTP_USER_AGENT= Moz...
>  REMOTE_ADDR= 203...
>  query= INSERT INTO sc...
> 
> Spacing is slightly different, so I modified the regex accordingly to
> (/^ query= (INSERT|UPDATE)/x) but it still found nothing.

The x modifier to this regular expression tells Perl to ignore the
whitespace characters. So you are trying to match the pattern
/^query=(INSERT|UPDATE)/. Since you have whitespace in your string,
there is no match.

Add the whitespace explicitly with \s:

  while(<$fh>) {
    if( /^\s* query \s* = \s* (INSERT|UPDATE)/x ) {
      print "$1$'";
    }
  }

The special variable $1 contains 'INSERT' or 'UPDATE', whichever
matched, and $' contains the rest of the line. This is one case where
the $' is useful despite the overhead of using it, since it simplifies
the program.


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

Date: 3 Aug 2004 08:34:30 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Parse log files for data
Message-Id: <cenimm$kfl$2@mamenchi.zrz.TU-Berlin.DE>

Dan  <agent@thwackspam.fathom.org> wrote in comp.lang.perl.misc:
> I have a bunch of custom log files with database queries in them, and
> would like to extract all the insert and update queries so I can
> recreate some lost data.
> 
> I'm pretty sure this is the perfect exercise for a perl script.  I
> just don't have the vocabulary to write it myself.  But this is all it
> needs to do:
> 
> * Open, in order, all log files named yyyymmdd.log in the current
> directory.  (where yyyymmdd is what you think it is)
> 
> * For each file, look at each line, and if it begins with " query=
> INSERT" or " query = UPDATE", write that entire line starting from the
> word "INSERT" or "UPDATE" to the output file.

Under Unix that's a job for (e)grep.  Untested:

    egrep -h '^query = (UPDATE|INSERT)' *.log >output

Anno


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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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 6815
***************************************


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