Deleting of Specific Rows.

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":

grep -v "$Title" database > database.tmp
mv database.tmp database

it did work. but the issue is , that it only deletes the "$Title" and not the entire row. I tried something out.

Title=
echo -n "Title:"
read Title  
#cat fruit | \
#sed -e '/apple/d' > fruit

grep -v "$Title , $2  , $3" fruit > fruit_temp
mv fruit_temp fruit

i tried to get it to read the 2 and 3 words but it does not seem to work. can sombody explain to me why?

Since your file is delimited by ":", then change your grep statement to:

grep -v "$Title : $2 : $3" fruit > fruit_temp

Nothing seemed to have happened. Am i writing the statements correctly?

Can you show us code you used to delete specific char?

okay, i used

T

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?

Reading and writing to the same file in a single command is not possible.

Okay, i solved that problem. what i am doing now is that i want to do a check to make sure that they are getting the information from the right row.

T

itle=
Price=
price_check=
echo -n "Title:"
read Title  
echo -n "Price:"
read Price
price_check = grep $Title fruit | grep $Price | awk -F":" '{print $3}' -line10
read price_check
if [$price_value = $Price] ; then

echo "Found the following record that matched:"

but i got an error message which reads

./delete: line 10: price_check: command not found

i was trying to put the value which i read into price_check. how else can i go about?

There's two things wrong with your price_check assignment statement.

  1. Remove or comment out "-line10". This isn't understood by the shell
price_check = grep $Title fruit | grep $Price | awk -F":" '{print $3}' #-line10
  1. Whenever you need to execute a command and use the results, you will need to use backticks (`)
price_check = `grep $Title fruit | grep $Price | awk -F":" '{print $3}'`

Once you make these changes it should run better.

You will also not need the read statement after it, and $price_value will need to be $price_check

#read price_check
if [$price_check = $Price] ; then
./delete: line 10: price_check: command not found
./delete: line 11: [: =: unary operator expected

i got this errors when i corrected my mistakes.

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
if [ "$price_check" = "$Price" ] ; then

There needs to be a space between [ and $ and using double quotes around the variables is a good idea.

Yeap, i did do that earlier but the same error which is ,

./delete: line 10: price_check: command not found

just keep appearing. Is the value of the price_check being read at all?

Hello gregarion.

You also have spaces here:

price_check = ...

Remove the spaces either side of the equal sign.

price_check=`grep $Title fruit | grep $Price | awk -F":" '{print $3}'`

I removed the spaces from your price_check assignment and got it to work. Try this:

price_check=`grep $Title fruit | grep $Price | awk -F":" '{print $3}'`

(You can ignore this, scottn is a faster typer than me)

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:

Hi.

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?