LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-29-2011, 03:13 PM   #1
KManepalli
LQ Newbie
 
Registered: Feb 2011
Posts: 4

Rep: Reputation: 0
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.
 
Old 04-29-2011, 03:42 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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,
 
Old 04-30-2011, 12:19 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
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.
 
Old 04-30-2011, 02:35 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Also, can't Perl do this? I don't see the need for grep.
 
Old 08-10-2011, 08:14 AM   #5
kevinpascal
LQ Newbie
 
Registered: Aug 2011
Posts: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by MTK358 View Post
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?
 
Old 08-10-2011, 09:13 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Quote:
Originally Posted by kevinpascal View Post
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.
 
  


Reply

Tags
perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lynx + Grep + Variable... thecurious1 Linux - General 10 03-13-2011 11:50 AM
How do you grep a variable? dbrazeau Programming 12 03-11-2010 09:57 PM
Grep variable with space brainlesseinstein Linux - General 3 08-10-2009 12:28 AM
Grep a file with a variable from an array..... OldGaf Programming 6 06-15-2008 07:16 PM
Using a variable containing a filename in grep TrumpetMan258 Programming 2 03-01-2008 02:27 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration