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

Hi , I am trying to grep for onefile here called "eni.cxx" , I am facing the follwing problem , the grep will return files like eniee.cxx or yeni.cxx which I don't need , so how do you do this in perl where I can make the grep command look only for eni.cxx :
my $file = "eni.cxx"; `grep $file ../nbs/*/bld/*.bld > $temp`;
thanks

Replies are listed 'Best First'.
Re: greping problem
by Zaxo (Archbishop) on Aug 13, 2002 at 18:32 UTC

    You appear to be confusing perl grep with the fine system utility of the same name. There are lightweight system greps written in perl, Super Search will find some here. You want the regular expression m/\beni\.cxx\b/ to match the given name and no other. System grep can manage a subset of perl regular expressions.

    After Compline,
    Zaxo

Re: greping problem
by fuzzyping (Chaplain) on Aug 13, 2002 at 18:27 UTC
    It's not grep, but I'm a big fan of using the right tool for the job. Try File::Find.

    -fp
Re: greping problem
by runrig (Abbot) on Aug 13, 2002 at 18:35 UTC
    It really depends on what the file contents look like. Do the lines of the file contain only a file name? Word boundaries as already suggested might work for you, but then you'd get 'abc-eni.cxx' as a false positive. Maybe you want a slash on the front, and whitespace on the back like  m#/eni.cxx\s#. Maybe you should split on '/' and see if the last element equals 'eni.cxx'.
Re: greping problem
by sschneid (Deacon) on Aug 13, 2002 at 18:27 UTC
    mmm, anchoring patterns.
    my $file = "eni.cxx"; if ($file =~ /\beni\.cxx\b/) { # do whatever }