in reply to Matching and extracting from file.

Ok, not a bad start. However you really, really, really should use strictures: use strict; use warnings;

The first problem is that you have provided a directory path string wiht \ characters - in Perl, as in C, those are quote characters and turn the following character into something magical. To get a single \ you need to "quote" it: \\. use strict; would generate the warning: "Unrecognised escape \d passed through".

The next problem is that /\s(\.){3}.*?)$/ has an extra ) in it. Again strictures would have picked that up for you.

After that, and having declared your various variables using my, all the static errors are cleaned up. What happens when you run? Now the warnings kick in. The first one is "Unrecognized escape \E passed through in regex; ...". You need to quote the file name that is interpolated directly into the regex: /^\Q$fname\E/i

Fix that and run again. The next warning is "... Use of uninitialized value in pattern match (m//) ...". Now there is a logic problem reveiled - what happens if the file name doesn't match? (In this case because of a trailing end of line character.) Fix that by changing the first if to skip if no match: next if $_ !~ /^\Q$fname\E/i;.

Ok, run again - runs with no errors or warnings, that's good. Doesn't produce any output, that's bad. At this point I give up because the file name being matched (C:\\EITV10CMD\\base\\detection\\basic_detection\\Filename\\DOIFFILE.COM) doesn't match any of the data lines. As A quick test I change the file name to something that will match and run again. Still no output.

Set a breakpoint and trace through with the debugger. Heh, that pesky regex we got the warning about is wrong. I didn't the comment and just removed the "extra" ). What it should look like is /\s\.{3}\s(.*)/. Fix that, run again, now we get some output

use strict; use warnings; my $fname = "C:\\EITV10CM\\base\\repair\\Companion_Repair\\File2SelfEx +t\\F2SEXT.COM"; while (<DATA>) { chomp; my $found; next if $_ !~ /^\Q$fname\E/i; chomp($_); /\s\.{3}\s(.*)/; #Trying to extract everthing after ... $found = $1; if ($found eq "is OK.") { my $virusname = "OK"; print "The virus detection name is $virusname\n"; } if ($found =~ /^Found: (.*?) NOT a virus[.]/) { my $virusname = $1; print "The virus detection name is $virusname\n"; } elsif ($found =~ /^Found the (.*?) (virus|trojan) !!!/) { my $virusname = $1; print "The virus detection names is $virusname\n"; #exit(); } elsif ($found =~ /^Found potentially unwanted program (.*?)[.]/) { my $virusname = $1; print "PuPs $virusname \n"; } elsif ($found =~ /Found (virus or variant|application) (.*?)( !!!| +[.])/) { my $virusname = $2; print "virus or variant $virusname \n \n"; } } __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. C:\EITV10CM\base\repair\Companion_Repair\File2SelfFile\F2SFILE.COM ... + Found the CMPAN/FIle2SelfFile virus !!! The virus has been removed from the file. Checking for another virus in the file ... C:\EITV10CM\base\repair\Companion_Repair\File2SelfFile\F2SFILE.COM ... + is OK.

prints:

The virus detection names is CMPAN/File2SelfExt The virus detection name is OK

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Matching and extracting from file.
by nisha (Sexton) on Mar 07, 2006 at 10:19 UTC
    A bunch of thanks :) It worked. Thanks, Nisha