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

Im trying to grep a variable which is used in an array to search in files...

if ( grep /$Check /i, <FILE> )

The check variable contains tags such as 1() 2() 3()

i want to match the whole tag but the following tags are getting grepped 1( 2 3(

I want the grep the whole tag ..i tried the following

if ( grep -w "$Check", <FILE> ) if ( grep -o "$Check", <FILE> )

but its stil not working

Replies are listed 'Best First'.
Re: Perl GREP
by Corion (Patriarch) on Jan 03, 2012 at 15:47 UTC

    Where in grep did you find the usage of grep -w "..." documented?

    Maybe you can show us a reduced version of your program, no longer than 15 lines, that still shows the problem? What is the value of $Check? What are three representative lines in FILE?

Re: Perl GREP
by runrig (Abbot) on Jan 03, 2012 at 15:51 UTC
    Parenthesis are regex meta characters and need to be escaped. You can easily do that with the quotemeta function or the "\Q" regex sequence:
    my $Check = quotemeta("1()"); if (grep /$Check/i, <FILE>) { ...
    That said, I hope the file is small, because slurping the entire contents of a file into memory is usually not a good strategy.
Re: Perl GREP
by kennethk (Abbot) on Jan 03, 2012 at 15:52 UTC
    If your variable contains metacharacters, you will need to escape them when you interpolate -- see Quoting metacharacters. Perhaps something like if ( grep /\Q$Check \E/i, <FILE> ) does what you mean?