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

Im trying to grep a keyword "function()" in many files and return the file names in which it is present...the problem is function() is in small letters whereas its in caps in the files i hav used /i to solve the issue..but currently im getting the files in which the following occurences are ther: function();

i dont want to extract function which is ending with semicolon any idea monks:

the snippet is:

if ( grep /$Function\(\)/i, <FILE> )

Replies are listed 'Best First'.
Re: Perl GREP
by marto (Cardinal) on Jan 04, 2012 at 12:24 UTC
Re: Perl GREP
by choroba (Cardinal) on Jan 04, 2012 at 12:23 UTC
Re: Perl GREP
by Marshall (Canon) on Jan 04, 2012 at 14:16 UTC
    Perl grep is way more powerful than Unix grep.
    Can you explain what you have tried re: command line grep and explain why this didn't work?

    One way to express an "and" with grep is just like the command line version, grep of a grep:

    if (grep{!/;$/}grep{/$Function\(\)/i}}<FILE>)
    or
    if ( grep {/$Function\(\)/i and !/;$/}<FILE>)

    Perl grep could have been called "filter", if the condition is true, the output is passed unchanged from the input (on the the right) to the output (on the left). The scalar form, like this one is, is the number of matches, not just simply a true or false condition. Here things that end in ';' are filtered out after they have satisfied the first condition.

    You can put any conditional you want within a Perl grep,
    grep {isMyMomsBirthdayTrue() and /SomeName/}@data;