For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > perl on windows 2000









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author perl on windows 2000

2006-01-10, 4:02 am


Hi. I'm working with Perl in a Windows environment. I'm having
trouble writing a script that reads a log file and writes out all of
the lines that match a certain expression into a file. I want to read
the value of the command-line argument and add to it to name the file.

Code:
#test.pl
$filename = shift;
print $filename; #this prints out ok
while (<> ) {

my ($host, $ident_user, $auth_user, $date, $time,
$time_zone, $method, $url, $protocol, $status, $bytes,
$agent, $referer) = /^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+)
([^\]]+)\] "(\S+) (.+?) (\S+)" (\S+) (\S+) "([^"]+)"
"([^"]+)"$/;

if ($url =~ /^\/somefolder\/.+?/) {
open (OUTFILE, ">>$filename.test") || die "Couldn't open
$filename.test for writing! \n";
#doesn't seem to be getting $filename
select OUTFILE;
select STDOUT;
print OUTFILE $_;
close OUTFILE;
}

}


Usage: test.pl myfilename.txt


------------------------------------------------------------------------
IT Interview Questions : [url]http://www.ginterview.com[/url] IT Tutorials and Articles : [url]http://www.garticles.com[/url] Oracle and Oracle Apps Training : http://www.exforsys.com
Chris

2006-01-10, 4:02 am

wrote:
> Hi. I'm working with Perl in a Windows environment. I'm having
> trouble writing a script that reads a log file and writes out all of
> the lines that match a certain expression into a file. I want to read
> the value of the command-line argument and add to it to name the file.


Can you please clarify? The above doesn't make sense.

You have several issues with your code, see below:

> Code:
> #test.pl


# you must always use these. They help with 99% of errors.
use strict;
use warnings;

> $filename = shift;


my $filename = shift;

> print $filename; #this prints out ok
> while (<> ) {
>
> my ($host, $ident_user, $auth_user, $date, $time,
> $time_zone, $method, $url, $protocol, $status, $bytes,
> $agent, $referer) = /^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+)

^^^
you need =~ here instead of =

> ([^\]]+)\] "(\S+) (.+?) (\S+)" (\S+) (\S+) "([^"]+)"
> "([^"]+)"$/;
>
> if ($url =~ /^\/somefolder\/.+?/) {


You'll never get here as $url won't match anything until you
correct the above regex.

> open (OUTFILE, ">>$filename.test") || die "Couldn't open
> $filename.test for writing! \n";


open (OUTFILE, ">>$filename.test") || die "Couldn't open
$filename.test for writing: $!"; # this informs the user
of why the file can't be opened and where the perl script dies.

> #doesn't seem to be getting $filename
> select OUTFILE;
> select STDOUT;


The second 'select' statement will over-ride the first.

> print OUTFILE $_;
> close OUTFILE;
> }
>
> }
>
>
> Usage: test.pl myfilename.txt
>
>
> ------------------------------------------------------------------------
> IT Interview Questions : [url]http://www.ginterview.com[/url] IT Tutorials and Articles : [url]http://www.garticles.com[/url] Oracle and Oracle Apps Training : http://www.exforsys.com

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com