LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using grep in perl with regex. (https://www.linuxquestions.org/questions/linux-newbie-8/using-grep-in-perl-with-regex-4175524619/)

jags1984 11-06-2014 11:10 PM

Using grep in perl with regex.
 
Using a regex in grep in perl code doesnt work.

Same is working in command line.

Code:

my PATTERN="fnOpenLog\([0-9a-zA-Z \*&]+,[0-9a-zA-Z \*&]+,[0-9a-zA-Z \*&]+\)" 

@srch = `grep -E -s -H -n -e $PATTERN "/home/SourceMAC/SourceMAC/src/test.txt"`;


The output of $#srch is -1 and the array @srch is null.

grail 11-06-2014 11:57 PM

What is the solution to this question seeing it is marked as SOLVED?

pan64 11-07-2014 12:14 AM

you should not use backtick and grep inside a perl script, because perl itself is capable to do the same thing.

jags1984 11-10-2014 05:48 AM

The Solution is :


Use single quote: It will not resolve the regex and assign to the variable as it is(like a constant)
Quote:

my PATTERN='fnOpenLog\([0-9a-zA-Z \*&]+,[0-9a-zA-Z \*&]+,[0-9a-zA-Z \*&]+\)'
Since we need to resolve it in grep use double quote for the Variable.

Quote:

@srch = `grep -E -s -H -n -e "$PATTERN" "/home/SourceMAC/SourceMAC/src/test.txt"`;


All times are GMT -5. The time now is 05:59 PM.