Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Acolyte needs regex help

by Corion (Pope)
on Jun 23, 2000 at 18:24 UTC ( #19585=note: print w/replies, xml ) Need Help??


in reply to Acolyte needs regex help

From what I see at first glance, you seem to be missing a + on the last (\S) sequence.

I always debug my REs by deleting enough stuff such that it matches again. Then I add step by step the other stuff back in.

In your case, you might want to rewrite the code such that it logs "bad" lines into a separate file (untested code !):

Update :Shendal noted below that I didn't fix the problem with the RE, and that's correct. I have now modified the RE so that it should match :)

#!/usr/bin/perl -w use strict; my $filename = "access.log"; my ($client,$identuser,$authuser,$date,$tz,$method,$url,$protocol,$sta +tus,$bytes,$refer,$platform,$extendedinfo) open( FILE, "< $filename" ) or die ("Couldn't open $filename : !$\n" ) +; while (<FILE>) { if ( /^(\S+) (\S+) (\S+) \[(\S+) (\S+)\] "(\S+) (\S+) (\S+)" (\S+) ( +\S+) "(\S+)" "(\S+) (.*?)"$/ ) { ($client,$identuser,$authuser,$date,$tz,$method,$url,$protocol,$st +atus,$bytes,$refer,$platform,$extendedinfo) = ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13); } else { print "Unmatched log entry : $_"; }; }; close FILE;

Replies are listed 'Best First'.
RE: Re: Acolyte needs regex help
by Shendal (Hermit) on Jun 23, 2000 at 18:35 UTC
    In your (admittedly untested) code, you didn't fix the \S+ in your regex. Wasn't that the point? :-)
RE: Re: Acolyte needs regex help
by Dungan (Initiate) on Jun 24, 2000 at 05:42 UTC
    Thanks you all much I came up with this as my final code <CODE> #! /usr/bin/perl -w use strict; my $goodFILE = "/logs/goodstring.txt"; my $badFILE = "/logs/badstring.txt"; my $logFILE = "/logs/processing/uexpress/access.20000622"; open(GOOD , ">$goodFILE") || die "cannot create $goodFILE : !$\n"; open(BAD , ">$badFILE") || die "cannot create $badFILE : !$\n"; open(FILE, "$logFILE"); while (<FILE>) { if ( /^(\S+) (\S+) (\S+) \[(^:+):(\d+:\d+:\d+) (\S+)\] "(\S+) (\S+) (\S+)" (\S+) (\S+) "(.*?)" "(.*?)"$/) { my ($client,$identuser,$authuser,$date,$time,$tz,$method,$url,$protocol,$status,$bytes,$refer,$platform) = ($1,$2,$3 ,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13); print GOOD "client : $client\n"; print GOOD "userid : $identuser\n"; print GOOD "authuser : $authuser\n"; print GOOD "date : $date\n"; print GOOD "time : $time\n"; print GOOD "time zone : $tz\n"; print GOOD "method : $method\n"; print GOOD "URL : $url\n"; print GOOD "protocol : $protocol\n"; print GOOD "status : $status\n"; print GOOD "byte
RE: Re: Acolyte needs regex help
by Dungan (Initiate) on Jun 24, 2000 at 05:44 UTC
    Thanks you all much I came up with this as my final code <CODE> #! /usr/bin/perl -w use strict; my $goodFILE = "/logs/goodstring.txt"; my $badFILE = "/logs/badstring.txt"; my $logFILE = "/logs/processing/uexpress/access.20000622"; open(GOOD , ">$goodFILE") || die "cannot create $goodFILE : !$\n"; open(BAD , ">$badFILE") || die "cannot create $badFILE : !$\n"; open(FILE, "$logFILE"); while (<FILE>) { if ( /^(\S+) (\S+) (\S+) \[(^:+):(\d+:\d+:\d+) (\S+)\] "(\S+) (\S+) (\S+)" (\S+) (\S+) "(.*?)" "(.*?)"$/) { my ($client,$identuser,$authuser,$date,$time,$tz,$method,$url,$protocol,$status,$bytes,$refer,$platform) = ($1,$2,$3 ,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13); print GOOD "client : $client\n"; print GOOD "userid : $identuser\n"; print GOOD "authuser : $authuser\n"; print GOOD "date : $date\n"; print GOOD "time : $time\n"; print GOOD "time zone : $tz\n"; print GOOD "method : $method\n"; print GOOD "URL : $url\n"; print GOOD "protocol : $protocol\n"; print GOOD "status : $status\n"; print GOOD "byte
RE: Re: Acolyte needs regex help
by Dungan (Initiate) on Jun 24, 2000 at 05:46 UTC
    Thanks much for the help <CODE>#! /usr/bin/perl -w use strict; my $goodFILE = "/logs/goodstring.txt"; my $badFILE = "/logs/badstring.txt"; my $logFILE = "/logs/processing/uexpress/access.20000622"; open(GOOD , ">$goodFILE") || die "cannot create $goodFILE : !$\n"; open(BAD , ">$badFILE") || die "cannot create $badFILE : !$\n"; open(FILE, "$logFILE"); while (<FILE>) { if ( /^(\S+) (\S+) (\S+) \[(^:+):(\d+:\d+:\d+) (\S+)\] "(\S+) (\S+) (\S+)" (\S+) (\S+) "(.*?)" "(.*?)"$/) { my ($client,$identuser,$authuser,$date,$time,$tz,$method,$url,$protocol,$status,$bytes,$refer,$platform) = ($1,$2,$3 ,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13); print GOOD "client : $client\n"; print GOOD "userid : $identuser\n"; print GOOD "authuser : $authuser\n"; print GOOD "date : $date\n"; print GOOD "time : $time\n"; print GOOD "time zone : $tz\n"; print GOOD "method : $method\n"; print GOOD "URL : $url\n"; print GOOD "protocol : $protocol\n"; print GOOD "status : $status\n"; print GOOD "bytes : $bytes\n";
RE: Re: Acolyte needs regex help
by Dungan (Initiate) on Jun 24, 2000 at 05:47 UTC
    Thanks much for the help <CODE> #! /usr/bin/perl -w use strict; my $goodFILE = "/logs/goodstring.txt"; my $badFILE = "/logs/badstring.txt"; my $logFILE = "/logs/processing/uexpress/access.20000622"; open(GOOD , ">$goodFILE") || die "cannot create $goodFILE : !$\n"; open(BAD , ">$badFILE") || die "cannot create $badFILE : !$\n"; open(FILE, "$logFILE"); while (<FILE>) { if ( /^(\S+) (\S+) (\S+) \[(^:+):(\d+:\d+:\d+) (\S+)\] "(\S+) (\S+) (\S+)" (\S+) (\S+) "(.*?)" "(.*?)"$/) { my ($client,$identuser,$authuser,$date,$time,$tz,$method,$url,$protocol,$status,$bytes,$refer,$platform) = ($1,$2,$3 ,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13); print GOOD "client : $client\n"; print GOOD "userid : $identuser\n"; print GOOD "authuser : $authuser\n"; print GOOD "date : $date\n"; print GOOD "time : $time\n"; print GOOD "time zone : $tz\n"; print GOOD "method : $method\n"; print GOOD "URL : $url\n"; print GOOD "protocol : $protocol\n"; print GOOD "status : $status\n"; print GOOD "bytes : $bytes\n";
RE: Re: Acolyte needs regex help
by Dungan (Initiate) on Jun 24, 2000 at 05:49 UTC
    Thanks much for the help <CODE> #! /usr/bin/perl -w use strict; my $goodFILE = "/logs/goodstring.txt"; my $badFILE = "/logs/badstring.txt"; my $logFILE = "/logs/processing/access.20000622"; open(GOOD , ">$goodFILE") || die "cannot create $goodFILE : !$\n"; open(BAD , ">$badFILE") || die "cannot create $badFILE : !$\n"; open(FILE, "$logFILE"); while (<FILE>) { if ( /^(\S+) (\S+) (\S+) \[(^:+):(\d+:\d+:\d+) (\S+)\] "(\S+) (\S+) (\S+)" (\S+) (\S+) "(.*?)" "(.*?)"$/) { my ($client,$identuser,$authuser,$date,$time,$tz,$method,$url,$protocol,$status,$bytes,$refer,$platform) = ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13); print GOOD "client : $client\n"; print GOOD "userid : $identuser\n"; print GOOD "authuser : $authuser\n"; print GOOD "date : $date\n"; print GOOD "time : $time\n"; print GOOD "time zone : $tz\n"; print GOOD "method : $method\n"; print GOOD "URL : $url\n"; print GOOD "protocol : $protocol\n"; print GOOD "status : $status\n"; print GOOD "bytes : $bytes\n"; %

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://19585]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2021-04-12 03:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?