Help searching a txt file for a Newbie!

They have: 6 posts

Joined: Jun 2000

Ok I am very new to writing perl however I know about programing and how it works. I need a program to search a log file from a proxy server in a txt format. I want to be able to specify which file to search and what string to search for. This will be run from the NT command prompt.Here is what I have so far from what I have read and tried.

#####################################################
# TxT Search v1 #
#####################################################
# By: Justin Congdon #
# #
#####################################################
print "Please enter the file you want to search: ";
$path = <STDIN>;
chomp $path;
open(PATH,$path) | | die "Error couldn't open $path, Error";
print "Please enter what you would like to search for: ";
$search = <STDIN>;
chomp $search;
$search =~ tr/A-Z/a-z/;
print "Searching....\n";
print "Your search found the following results:\n";
print "\n";
while(<PATH> ){
print if (/$search/);
}

Thats it. It returns result but how do I format them and make them look descent.
The log file is in the following format. Every line is very long so you can imagine what the output looks like.

111.111.111.111, ****\USER_BOB, Mozilla/4.0 (compatible; MSIE 4.01; Windows 98), Y, 00/00/00, 9:07:25, Proxy, PROXY, -, www.somesite.com, 111.111.111.111, 80, 188, 1486, 2692, http, tcp, GET, www.somesite.com, text/html, Inet, 200, 1073872897

Thanks

[This message has been edited by jleecong (edited 09 June 2000).]

They have: 568 posts

Joined: Nov 1999

Read http://www.cpan.org/doc/manual/html/pod/perlre.html it's a good tutorial on regular expressions. Since your pretty new to Perl you probably don't know what those are so I really suggest you read them.

They have: 6 posts

Joined: Jun 2000

I have looked at them quit a few times It is very hard learning something that you can do 5 different ways. I have tried lots of differnt stuff however they either don't work or are not the results that I want. Anyway this is the code that I am using now.

#####################################################
# TxT Searcher v1.1 #
#####################################################
# By: Justin Congdon #
# #
#####################################################
print "Please enter the file you want to search: ";
$path = <STDIN>;
chomp $path;
open(PATH,$path) | | die "Error couldn't open $path, Error";
@Path = <PATH>;
print "Please enter what you would like to search for: ";
$search = <STDIN>;
chomp $search;
$search =~ tr/A-Z/a-z/;
print "Searching....\n";
@results = grep {/$search/} @Path;
foreach $List (@results){
($ip,$usr,$env,$y,$date,$when,$prox,$prox1,$****,$add,$ip1,$****1,$****2,$****3,$****4,$****5,$****6,$****7,$fadd,$type,$****8,$****9,$****10) = split(/,/,$List);
print "\nYour search found the following results:\n\n$usr |$add |$when |$date \n\n" if ($List);
}
I formated the output some. But would like the results to look cleaner. I also need a if,else statment so if nothing is found in the file it will tell you instead of just going to the C prompt and if something is found it won't keep repeating (Your search found lalalala....)

Thanks

[This message has been edited by jleecong (edited 09 June 2000).]

[This message has been edited by jleecong (edited 09 June 2000).]

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.