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

Hi Monks! this here newbie is trying to find the solution to what I think should be an easy problem to solve: I need to do a match search on a local flat text file that contains html. If the file contains the pattern, in this case "File Error" - then continue... I have tried a couple things and I am lost. here is a snippet from my most recent failed attempt. (dont laugh plz)
$vchpage = `cat $tmp`; if ($vchpage =~ /File Error/gi){
if someone could give me a quick piece of code that would search the local file named "$tmp" for "File Error" I would be extremely grateful!

Replies are listed 'Best First'.
Re: search match within flat text file
by grep (Monsignor) on May 31, 2002 at 01:27 UTC
    $vchpage = `cat $tmp`; if ($vchpage =~ /File Error/gi){
    Works for me
    Sounds like you have a problem with what you think you should be expecting.
    Print out the contents of $vchpage. If it still looks right, then check for a non-printable char or extra spaces between 'File' and 'Error' in the output of $vchpage.

    You should also look at changing your strategy, using 'cat' to run your file into a scalar is not very portable (M$ and Mac do not have 'cat'.), it also decreases your error reporting (what if you don't have permissions?), it also opens you up to strange errors (what if the new version of cat does something strange?)

    look at opening the file and stuffing it into a scalar and searching with your regex, or reading the file into an array and greping it on your regex. These are safer solutions.

    UPDATE: well Mac OS/X has 'cat' - still not used to a Mac with a real OS :)



    grep
    These are not the monks you are looking for, move along
Re: search match within flat text file
by itsmrchris (Initiate) on May 31, 2002 at 02:41 UTC
    thanks for the help guys... turns out the code was working after all like grep suggested, I printed the results of the `cat $tmp` to troubleshoot it, and it was working fine. my problem was in another line of code. I was unsure if I could use "cat" with a scalar - vs- an array... but now I have a new question on the board! lol thanks again
Re: search / match within flat text file
by rbc (Curate) on May 31, 2002 at 00:07 UTC
    you want to use grep.
    try perldoc -f grep
    ... #don't do `cat ...` open( FH, "<flatfile.txt") or die "Can't open flatfile.txt:$!\n"; my @matches = grep /File Error/, <FH>; if ( $#matches > -1 ) { print "Got " . ($#matches + 1) . " matches\n"; }
    Hope that helps.
Re: search match within flat text file
by particle (Vicar) on May 31, 2002 at 01:41 UTC
    i'm assuming memory's not a problem here, since i've never seen html files larger than a few hundred kilobytes. this will slurp the file into a scalar, and match against the scalar. i have a feeling this is faster than matching against every line. i don't know if this will meet all your requirements, because i'm not sure what "then continue..." means.

    #!/usr/bin/perl -w use strict; my $tmp = "/path/to/my.html"; my $pattern = qr/File Error/i; my $html; { local *INFILE; open( INFILE, $tmp ) or die "ack! $!"; -f INFILE && sysread( INFILE, $html, -s INFILE ); } die "$tmp: $pattern not found" unless $html =~ $pattern; # then continue...

    ~Particle *accelerates*

Re: search match within flat text file
by wageslave (Novice) on May 31, 2002 at 01:07 UTC
    basically whats happening is that $vchpage is getting set as "cat $tmp" what you proably want is something along this line
    use strict; $tmp="some file"; open (FILE,"$tmp"); while(<FILE>) { if (/file error/i) {print "error in file";} else {print"@_ no error";} }
    which basically opens up the file goes thru one line at a time
      please use die or warn with open, or at least check the return status. otherwise you're lible to encounter subtle errors in your code.

      ~Particle *accelerates*

      basically whats happening is that $vchpage is getting set as "cat $tmp"

      Nope - he's setting $vchpage to the result of the command 'cat filename'. ('cat' is like 'type' on M$/DOS - it lists the file to the STDOUT)

      The (backtick) ` operator returns the result of the command. It is not a quote operator like (quote) ' and (doublequote) ".

      This is explained on the perlop man page under Quote and Quote-Like operators



      grep
      These are not the monks you are looking for, move along