nisha has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl monks, I have an issue w.r.t to searching for an entry in a file... I haver text file of the format:
C:\EITV10CM\base\repair\Companion_Repair\File2File\F2F.COM ... is OK. C:\EITV10CM\base\repair\Companion_Repair\File2SelfExt\F2SEXT.CMP ... i +s OK. C:\EITV10CM\base\repair\Companion_Repair\File2SelfExt\F2SEXT.COM ... F +ound the CMPAN/File2SelfExt virus !!! The virus has been removed from the file. Checking for another virus in the file ... C:\EITV10CM\base\repair\Companion_Repair\File2SelfExt\F2SEXT.COM ... i +s OK
Below is the piece of code which was written by me...
#!/usr/bin/perl use strict; use warnings; #Program to read the command line report file; and extract the virus d +etection name for a file. my $cmdrep = "d:/eitv10cm_cln.rep"; my $fname = "D:\\EITV10CM\\base\\repair\\Companion_Repair\\File2SelfEx +t\\F2SEXT.COM"; my ($found,$virusname,$flag,$i); (@dirtemp) = split /:/,$fname shift @dirtemp; foreach $itemp ( @dirtemp ) { $filepath .= $itemp; } open FH, "$cmdrep" or die "Cannot open $cmdrep. \n"; while (<FH>) { chomp; next if $_ !~ /^\Q$filepath\E/i; chomp($_); /\s\.{3}\s(.*)/; $found = $1; if ($found eq "is OK.") { $virusname = "OK"; print "The virus detection name is $virusname\n"; last; } if ($found =~ /^Found: (.*?) NOT a virus[.]/) { $virusname = $1; print "The virus detection name is $virusname\n"; last; } elsif ($found =~ /^Found the (.*?) (virus|trojan) !!!/) { $virusname = $1; print "The virus detection names is $virusname\n"; last; } elsif ($found =~ /^Found potentially unwanted program (.*?)[.] +/) { $virusname = $1; print "PuPs $virusname \n"; last; } elsif ($found =~ /Found (virus or variant|application) (.*?)( +!!!|[.])/) { $virusname = $2; print "Virus or variant $virusname \n \n"; $flag =1; last; } } close(FH); }
Since the drive letters in the variable and text file are the only ones which vary, i have tried to remove the drive letter from $fname; this doesnt match in the text file at all even though the remaining of the string is the same. Could u please help me with this...dont know how to go about doing it.

Replies are listed 'Best First'.
Re: Problem with searching for an entry in a file
by GrandFather (Saint) on Mar 17, 2006 at 11:21 UTC

    Although you have use strict; at the top of your code, the line (@dirtemp) = split /:/,$fname can not be right. It is missing a semicolon at the end of the line and my off the front. There are other similar errors.

    That aside, your problem is next if $_ !~ /^\Q$filepath\E/i; trys to match from the start of the line (the ^ in the regex). Instead use /\Q$filepath\E/i; for the regex.


    DWIM is Perl's answer to Gödel
Re: Problem with searching for an entry in a file
by prasadbabu (Prior) on Mar 17, 2006 at 11:23 UTC

    Hi nisha, You have used strict and warnings but you have not declared the variables properly. Also you have splitted with ':' but you have not removed that from the file content because you are using '^' anchor, else you remove that anchor.

    use strict; use warnings; #Program to read the command line report file; and extract the virus d +etection name for a file. my $cmdrep = "d:/eitv10cm_cln.rep"; my $fname = "D:\\EITV10CM\\base\\repair\\Companion_Repair\\File2SelfEx +t\\F2SEXT.COM"; my ($found,$virusname,$flag,$i); my (@dirtemp) = split /:/,$fname; #declared the variables with my shift @dirtemp; my $filepath; foreach my $itemp ( @dirtemp ) { $filepath .= $itemp; } while (<DATA>) { chomp; $_ =~ s/\w\://; #you are missing this line next if $_ !~ /^\Q$filepath\E/i; #else remove the anchor chomp($_); /\s\.{3}\s(.*)/; $found = $1; print "$found\n"; if ($found eq "is OK.") { $virusname = "OK"; print "The virus detection name is $virusname\n"; last; } if ($found =~ /^Found: (.*?) NOT a virus[.]/) { $virusname = $1; print "The virus detection name is $virusname\n"; last; } elsif ($found =~ /^Found the (.*?) (virus|trojan) !!!/) { $virusname = $1; print "The virus detection names is $virusname\n"; last; } elsif ($found =~ /^Found potentially unwanted program (.*?)[.] +/) { $virusname = $1; print "PuPs $virusname \n"; last; } elsif ($found =~ /Found (virus or variant|application) (.*?)( +!!!|[.])/) { $virusname = $2; print "Virus or variant $virusname \n \n"; $flag =1; last; } } __DATA__ C:\EITV10CM\base\repair\Companion_Repair\File2File\F2F.COM ... is OK. C:\EITV10CM\base\repair\Companion_Repair\File2SelfExt\F2SEXT.CMP ... i +s OK. C:\EITV10CM\base\repair\Companion_Repair\File2SelfExt\F2SEXT.COM ... F +ound the CMPAN/File2SelfExt virus !!! The virus has been removed from the file. Checking for another virus in the file ... C:\EITV10CM\base\repair\Companion_Repair\File2SelfExt\F2SEXT.COM ... i +s OK output: ------- Found the CMPAN/File2SelfExt virus !!! The virus detection names is CMPAN/File2SelfExt

    Prasad