LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-14-2010, 05:58 AM   #1
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Rep: Reputation: 15
Searching using grep


okay, i seem to have a problem with the search command which i used.

Code:
echo -n "Title:"
read Title  
echo -n "Author:"
read Author
price_check=`grep $Title fruit | grep $Author | awk -F":" '
if [ "$price_check"="$Author" ] ; then
  echo "book exist"
else
 echo " Book does not exist"
fi
{print $3}'
`

Code:
Persia:42:John      ---database
France:50:Mabel 
Persia:50:Rach
Germany:60:John
for example, the user is to input title as "Persia" and "Author" as John. i will then use grep to search for lines which contain the title, then pipe it into another grep command which will then search for author based on the row available, then after that print $3 right?


but for some reason, when i did a check on it, it does not seem to work. when i input data which is not in the database, it still displays the message "book exist" instead of telling me "book does not exist".

What is the correct format or coding to be used to do a search based on two inputs?

Last edited by gregarion; 01-14-2010 at 07:47 AM.
 
Old 01-14-2010, 06:24 AM   #2
SeymourButts
LQ Newbie
 
Registered: Jan 2010
Posts: 28

Rep: Reputation: 15
Did you try dropping the dbl quotes?

Code:
if [[ $price_check = $Author ]] ; then
 
Old 01-14-2010, 07:12 AM   #3
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
did not help. still the same result.
 
Old 01-14-2010, 07:20 AM   #4
SeymourButts
LQ Newbie
 
Registered: Jan 2010
Posts: 28

Rep: Reputation: 15
You may also want to look at this line closely. The punctuation marks don't match up. In front of grep you have an acute, while at the end of the line you have a single quote.

Code:
price_check=`grep $Title fruit | grep $Author | awk -F":" '
And, what exactly is "fruit".
 
Old 01-14-2010, 07:27 AM   #5
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
i tried using the if statement :

Code:
awk  '{if ($1==$Title && $3==$Author) print $3 } ' fruit
but there does not seem to be working. am i using the "if" statement wrongly?
 
Old 01-14-2010, 07:44 AM   #6
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
Sorry, fruit is the name of my database. and for the single quote, let me correct it. the code looks like this :

Code:
price_check=`grep $Title fruit | grep $Author | awk -F":" '
 
Old 01-14-2010, 07:55 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I don't see that you changed it---it should be backtics at both ends..

BUT: This syntax is obsolete---the current advice is to do it this way:
Code:
price_check=$(grep $Title fruit | grep $Author | awk -F":")
For your question about the if statement: AWK syntax is:
pattern {action}
so it looks like you have "{" in the wrong place.
 
Old 01-14-2010, 08:25 AM   #8
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
Code:
echo -n "Title:"
read Title  
echo -n "Author:"
read Author

price_check=$(grep $Title fruit | grep $Author | awk -F":"'{print $2}')

echo $price_check
Okay, am i correct to say that there should be a print $3 there as it is where the Author columm is? so basically, this is suppose to check for line which contain $title , then with that info, check for lines which contain $Author , and then with the row which we have , print out $2 which should be the price. the echo command will then show the price as an output right?

that is what i did, but nothing seems to be working.

could you show me an example how the if statement should be used. i tried out changing the location of the { from :
Code:
price_check=`awk  -F: '{if ($1=='$Title' && $3=='$Author') print $3 } ' fruit`
to
Code:
price_check=`awk  -F: 'if ($1=='$Title' && $3=='$Author') { print $3 } ' fruit`
but it does not seem to work.
 
Old 01-14-2010, 08:53 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I can't relate your first question to the preceding discussion,

In the "price_check" statement, why do you have quotes on $Title and $Author? I think the resulting test would be--e.g.: Does the value of variable 3 equate to the literal string "$Author"
 
Old 01-14-2010, 09:00 AM   #10
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
yes, that what i am trying to achieve. im trying to do a test to check if we are going to delete the information on the right row. i guess the best way to do it would be to run pull out a data from the row which you have specified and then compare it to the user input.


Do you know of any other ways in which i do this search?
 
Old 01-14-2010, 09:10 AM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Sorry---I am not connecting your answers with my questions....

Are you trying to compare the value of variable "3" with the literal string "$Author"? I don't think so---I think you want the value of the variable named "Author".

If I'm correct, then you want--e.g.: if ($3==$Author)
 
  


Reply


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
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
grep, sed, awk or tr - searching words in a string hal8000b Programming 2 03-06-2009 08:04 PM
searching within files. cat grep find w3bd3vil Linux - Newbie 4 10-13-2008 03:59 AM
Searching in files: How do I grep better? Elomis Linux - General 5 12-19-2007 01:52 AM
grep searching for control chars shell issues gquiring Linux - General 2 05-02-2005 02:25 PM


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

Main Menu
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