Fruit : Price : Quantity
apple : 20 : 40
chiku : 40 :30
Hey guys, i have written a code using sed to delete a specific char which is being typed in. But the problem i am having is , how can i expand my coding to actually allow it do delete the whole row. For example, if i type in apple, i would like it to delete the other information about apple such as price and quantity.
Title=
echo -n "Title "
read Title
cat database | \
sed -e '/ "$Title" /d' > database
Can sombody help me in giving me a headstart on how i am able to expand this coding to include it to delete the entire row?
One way to do this would be to write all lines except the ones containing $Title to a temp file, then rename the temp file to the original. You can do this using "grep -v":
itle=
echo -n "Title:"
read Title
#grep -v "$Title" fruit > fruittemp
#mv fruittemp fruit
Actually this can be used to solve my problem because it does delete all the information in that row. but can sombody explain to why must be send the output to a new file and then rename it? cant we just send the output to the same file?
you got it to work? what was your display result? i pasted in your coding and i am still not getting a response. when i type in the name and the price, it just ask me to input another command.
#!/bin/bash
Title=
Price=
price_check=
echo -n "Title:"
read Title
echo -n "Price:"
read Price
price_check=`grep $Title fruit | grep $Price | awk -F":" '{print $3}'`
if [ "$price_check" = "$Price" ] ; then
echo "Found the following record that matched:"
grep $Title fruit | grep $Price
fi
Am i getting anything wrong here??
---------- Post updated at 03:32 AM ---------- Previous update was at 03:31 AM ----------
Okay guys, i got it. its the spacing which always seems to kill me. thanks alot people. really appreciate the help 2pugs.
Make sure your fruit file is in the same directory since there isn't a path specified.
$ cat fruit
apple : 20 : 30
$ cat test.bash
#!/bin/bash
Title=
Price=
price_check=
echo -n "Title:"
read Title
echo -n "Price:"
read Price
price_check=`grep $Title fruit | grep $Price | awk -F":" '{print $3}'`
if [$price_check = $Price ] ; then
echo "Found the following record that matched:"
# grep $Title filename | grep $Price
fi
$ ./test.bash
Title:apple
Price:30
Found the following record that matched:
Either remove the spaces from your input file (fruit), or change this line:
if [ "$price_check" = "$Price" ] ; then
To:
if [ $price_check -eq $Price ] ; then
$ ./Test
Title:apple
Price:40
Found the following record that matched:
apple : 20 : 40
Edit: If you have an input file colon-separated, then the spaces become part of the fields. This means you always have to code around them. Life, as 2pugs said, would probably be easier without them.
I agree with scottn that removing the spaces from your input file would probably make your life easier. Scripting can be painful in the beginning but like anything, the more you practice the easier it becomes.
True. i always wonder what went wrong with my coding, and when i check, its always the space. One more question about awk. NR is suppose to give you the number of records in a database for example :
Title : Price : Author
C++ 20 John
PHP 25 Gorer
VB 30 John
I did some coding to search and display the information either based on author or Title.
#Title=
#Author=
echo -n " Press a to search by Author or b to search by title:"
read Menu
if [ $Menu = "a" ] ; then
echo -n "Enter Author:"
read Author
awk '/'$Author'/' fruit
elif [ $Menu = "b" ] ; then
echo -n "Enter Title:"
read Title
awk '/'$Title'/' fruit
else
echo "It is not displayed"
fi
What i am curious is, how am i able to count the number of records being found. For example, if i were to search for Author , i would get 2 hits for John. I guess i can use NR , which is suppose to count the number of records being displayed. I tried putting in a search pattern into the if statement for awk like this :
awk 'END {if ($3 = James {print NR}' fruit
since author is in the $3 , i tried doing a search and if it matched the author name, i would then ask it to print. I tried running it but nothing happens. What is my mistake?