LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-05-2010, 12:39 PM   #1
u_Muhammad
LQ Newbie
 
Registered: Jun 2010
Posts: 13

Rep: Reputation: 0
can we do pattern macting based on a variable ??


i have the follwing code
touch umer.txt
fdisk -l | grep 'Disk /' umer.txt
read file
#now i want to check the 'file' string exits in the umer.txt or not
#i have
cat umer.txt | grep $file


but it is not workig...
how to do that ????

Last edited by u_Muhammad; 06-05-2010 at 12:42 PM.
 
Old 06-05-2010, 01:09 PM   #2
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Simple answer, yes we can.

Have you checked that there's anything in the file umer.txt?

Quote:
fdisk -l | grep 'Disk /' umer.txt
This does nothing with the output of the fdisk command, and instad greps umer.txt for 'Disk /'. It should, I think, be:

Code:
fdisk -l | grep 'Disk /' > umer.txt # > is the redirect operator
Also, what pattern are you searching for (i.e. what are you entering at the "read" line)?

Last edited by Robhogg; 06-05-2010 at 01:35 PM. Reason: corrected tag
 
Old 06-05-2010, 01:23 PM   #3
u_Muhammad
LQ Newbie
 
Registered: Jun 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Yes sir, afer this cammad.
The file umer.txt contain
'Disk /dev/sda' and some other info.
now i want to check that if the user give input which is not in the umert.txt. then i will not work.
like if he enter abc then failed but if enters /dev/sda then continue.
 
Old 06-05-2010, 01:38 PM   #4
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
So, are you wanting to do something like this?:

Code:
if ! cat umer.txt | grep $file; then 
      exit 1 # exit from script if no match
fi
It might help if you explain what it is that isn't working.
 
Old 06-05-2010, 01:59 PM   #5
u_Muhammad
LQ Newbie
 
Registered: Jun 2010
Posts: 13

Original Poster
Rep: Reputation: 0
sir its not working....
it gives :
grep [option] pattern [File]
 
Old 06-05-2010, 02:10 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Please post the actual commands you are using, and the resulting output (cut and paste from the terminal). Also, put the code into [code] tags. (Advanced mode--select the text, and click on #)
 
Old 06-05-2010, 02:11 PM   #7
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Quote:
Originally Posted by u_Muhammad View Post
grep [option] pattern [File]
Which means, I believe, that what you are entering is not a valid pattern for grep.

Could you give an example of what you are entering, please, and the full output.
 
1 members found this post helpful.
Old 06-05-2010, 02:47 PM   #8
u_Muhammad
LQ Newbie
 
Registered: Jun 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Sir, i have done it. Actually i was putting it in double quotes i think thats why it is giving error.
Thank you for your kind attention.
 
  


Reply



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
bash search for a pattern within a string variable nutthick Programming 8 03-17-2015 07:26 PM
Script to copy specific directory based on variable to folder with that variable name fluxburn Programming 7 01-07-2010 07:59 PM
Bash: Pattern matchin - how to do matching on a variable? SQADude Programming 23 12-09-2009 01:47 AM
Replacing a pattern using variable? shivarajM Linux - Software 1 04-30-2009 01:23 AM
Replace a var based on a pattern OldGaf Programming 4 10-16-2008 06:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:52 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
Open Source Consulting | Domain Registration