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!

Number matching 2

Status
Not open for further replies.

nrich239

Programmer
Jun 9, 2004
22
US
I have a huge text file with info about when people login and my super, wants a perl script to be able to find certain people based on serial #, user name, or view all within a range of dates.
For some reason I cannot get it to find anything inside of the text.
I have anoter script that finds when the login was rejected and this is based off that one but this one fails.

Any help would be appreciated,
Nate

an example line of the text file is:
303426,11/10/2003,03:30:30,11/09/2003,22:30:30,1011,"Person's Name","userID","serial#(12 digit number)","","","5hint-vp01","psysadm-rsa01p","Person's Name",0,0,"Access DENIED"

the program as is:
#!c:\perl\perl.exe

print 'Enter 1 to print to screen, 2 to print to a file:';
$answer = <STDIN>;
print 'Enter 1 to search by Serial #, 2 to search by userID, 3 to search by date range:';
$answer2 = <STDIN>;
if($answer2 == 1) {
print 'Enter Serial #: ';
$serial = <STDIN>;
print "$serial";
}
elsif($answer2 == 2) {
print 'Enter the userID: ';
$user = <STDIN>;
print "$user";
}
else{
print 'Enter the beginning date: ';
$begin = <STDIN>;
print 'Enter the end date: ';
$end = <STDIN>;
print "$begin";
print "$end";
}
$filename = 'erslog2';
$filename2 = 'results';
open(FILE, "$filename");
open(FILE_HANDLER, ">$filename2");
@lines = <FILE>;
foreach $word (@lines) {
if($answer == 1) {
if($answer2 == 1) {
if($word =~ $serial) {
print "$word ";
}
}
elsif($answer2 == 2) {
if($word =~ $user) {
print "$word ";
}
}
}
elsif {
if($answer2 == 1) {
if($word =~ $serial) {
print FILE_HANDLER "$word ";
}
}
elsif($answer2 == 2) {
if($word =~ $user) {
print FILE_HANDLER "$word ";
}
}
}
else
print "Feature not implemented yet";
}
if($answer == 2) {
print "Everything is in a file called results ";
}
 
This could be greatly simplified.. it's a little hard to follow with all the if/elsif statements. Also, if it's a huge log file, you should process it one line at a time or use Tie::File instead of loading the whole thing into an array.
I think I would use Tie::File, then grep() on the tied array.
I can post an example when I get a chance.
 
I'm a native java programmer is why the program looks like it does.
I'm not used to this one or two lines doing all the work kind of thing yet.

Nate
 
I didn't have time to get any kind of date range search into this, but searching for multiple terms seems to work if you separate them with a pipe(or)
ex: 11/10/2003|11/11/2003
(I should have done validation of $outputOpt right after it's entered instead of after the tie)
Hopefully this helps, though

Code:
use Tie::File;
use Fcntl 'O_RDONLY';

$inputFile  = 'erslog2';
$outputFile = 'results';

print 'Enter 1 to print to screen, 2 to print to a file: ';
chomp($outputOpt = <STDIN>); #chomp removes newline from input string
print 'Enter Serial# or userID to search for: ';
chomp($searchPattern = <STDIN>);

tie @lines, 'Tie::File', $inputFile, mode => O_RDONLY or die "Can't open $inputFile for reading: $!\n";

if (@results = grep(/$searchPattern/,@lines)){
    
    if ($outputOpt == 1){
        print join("\n",@results);
    }
    elsif ($outputOpt == 2){
        open(OUTPUT, ">$outputFile") or die "Can't open $outputFile for writing: $!\n";
        print OUTPUT join("\n",@results);
        close(OUTPUT);
        print "Everything is in a file called $outputFile\n";
    }

}       
else {
    print "No match\n";
}
 
try using the chomp function on the input from <STDIN>.

This will remove the newline character which is automatically captured along with the input. I think maybe your inputs aren't matching because the variable you compare against has the newline.

ie. chomp($answer = <STDIN>);

I think that should do the trick.
 
Thank you to both, you've saved me multiple hours of frustration.

Thanks again,
Nate
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top