Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Log parsing 1

Status
Not open for further replies.

fmuquartet

Programmer
Sep 21, 2006
60
US
Greetings,
I am wanting to parse my Apache access logs for and empty string that appears in the same position and remove them from my access logs as they are just noise.

example:
192.168.15.101 - - [02/Jul/2008:10:28:26 -0500] "GET

In short I am wanting to remove all hyphen in the 3rd position from all my access logs, is this possible using Perl?

Thanks,
 
I tried something like, 'perl -pi -e 's/-//g' access.log, but the line contain the '-' hyphen is not being removed. So what I am wanting, is every LINE that has '-' in the 3rd position to get moved.

-Thanks
 
This is what I tried.

Code:
#!/usr/bin/perl
use strict;

while (<DATA>){
   $_ =~ s/-\s-//g;
   print $_;
}

__DATA__
192.168.15.101 - - [02/Jul/2008:10:28:26 -0500] "GET

Results in:
192.168.15.101 [02/Jul/2008:10:28:26 -0500] "GET

If you want a specific postion, you can look into substr
 
This is great, except for I am wanting to REMOVE any line in the access log which contains a '-' in the 3rd position completely from the access.log.

so if I have multiple entries like this:
192.168.15.101 - - [02/Jul/2008:10:28:26 -0500] "GET
192.168.15.101 - - [02/Jul/2008:10:28:26 -0500] "GET
192.168.15.101 - - [02/Jul/2008:10:28:26 -0500] "GET
192.168.15.101 - - [02/Jul/2008:10:28:26 -0500] "GET
192.168.15.101 - - [02/Jul/2008:10:28:26 -0500] "GET
192.168.15.101 - - [02/Jul/2008:10:28:26 -0500] "GET

I want the NOT appear in my access.log.

-Thanks
 
Perl:
use strict;
use warnings;

while (<>) {
   my @fields = split;
   print unless $fields[2] eq '-';
}

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Steve I will give this a try and let you know my findings!

Many thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top