in reply to Re: Online GREP problem
in thread Online GREP problem

Interestingly enough, the file is never opened. There are 4 grep calls before my altered one. Any reason that the grep line is enclosed in ``? here are the previous calls.
if($aquiSW =~ /New Stuff/i) {`grep -i \"1\$\" $DataIn > $DataTmp`; $DataIn=$DataTmp; if($DataTmp eq $DataTmpa){$DataTmp=$DataTmpb;} # if else{$DataTmp=$DataTmpa;} # else } # if aquiSw if($lA && $Artist ne ".*") {`grep -i \"\^$Artist\" $DataIn > $DataTmp`; $DataIn=$DataTmp; if($DataTmp eq $DataTmpa){$DataTmp=$DataTmpb;} # if else{$DataTmp=$DataTmpa;} # else } # if $IA if($lL && $Label ne "*"){`grep -i \"\|$Label\" $DataIn > $DataTmp`; $DataIn=$DataTmp; if($DataTmp eq $DataTmpa){$DataTmp=$DataTmpb;} # if else{$DataTmp=$DataTmpa;} # else } # if $IL if($lN && $LabelNo ne "*"){`grep -i \"\|$LabelNo\" $DataIn > $DataTmp` +; $DataIn=$DataTmp; if($DataTmp eq $DataTmpa){$DataTmp=$DataTmpb;} # if else{$DataTmp=$DataTmpa;} # else } # if $IN

Replies are listed 'Best First'.
Re^3: Online GREP problem
by almut (Canon) on Apr 05, 2010 at 21:26 UTC
    Interestingly enough, the file is never opened.

    The reason is that the external program 'grep' is opening the file itself.

    Any reason that the grep line is enclosed in ``?

    Backticks (``) run an external program, like the grep here.

    My suggestion was to not use backticks at all, as Perl is perfectly capable of doing grep-like jobs all by itself...  and probably a lot faster, too, than when calling the external grep multiple times.  Of course, you'd have to restructure the program accordingly...

    AFAICT, the consecutive runs of grep (in combination with the use of the temp files) achieve to extract lines where all the tested conditions match.  A restructured solution without using grep could look something like this:

    ... LINE: while (<$fh>) { if ($aquiSW =~ /New Stuff/i) { next LINE unless /1$/; } if ($lA && $Artist ne ".*") { next LINE unless /^\Q$Artist/i; } if ($lL && $Label ne "*") { next LINE unless /\|\Q$Label/i; } # ... if ($lT && $Title ne "*") { # split $Title into @words... for my $word (@words) { next LINE unless /\Q$word/i; } } # ... # we only get here if all the tested conditions matched print; }