LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using Variable in grep in perl (http://www.linuxquestions.org/questions/linux-newbie-8/using-variable-in-grep-in-perl-877881/)

KManepalli 04-29-2011 02:13 PM

using Variable in grep in perl
 
Hi

I have a situation where i have a file with values in that, Something like below.

12345
67890
35790

I have a variable "$ELEMENT" holding these values, Now i need pass these variable values in grep statement to get messages having these numbers from one file(text1.dat) and create new one(text2.dat).

Below is my grep statement in perl.

system("grep -h \"$ELEMENT\" text1.dat >> test2.dat");

I know the grep statement in not true, Because i cannot pass variable there. I am not sure how to use variable in grep in perl code.Any suggestions??

Thanks
K.

Snark1994 04-29-2011 02:42 PM

A better way to do it would just be:

Code:

`grep -h $ELEMENT text1.dat >> test2.dat`
The backticks avoid the need to use the system() call :)

Hope this helps,

grail 04-29-2011 11:19 PM

I guess the question I have is, does the perl script only do this task? Just seems grep could well do the task on its own.

MTK358 04-30-2011 01:35 PM

Also, can't Perl do this? I don't see the need for grep.

kevinpascal 08-10-2011 07:14 AM

Quote:

Originally Posted by MTK358 (Post 4342099)
Also, can't Perl do this? I don't see the need for grep.

Perl also has a grep function. Can't we simply use perl grep function?

MTK358 08-10-2011 08:13 AM

Quote:

Originally Posted by kevinpascal (Post 4438649)
Perl also has a grep function. Can't we simply use perl grep function?

Why did you bump a dead thread just to repeat what the last post said?

Reported.


All times are GMT -5. The time now is 03:20 AM.