• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

Delete a record in shell program.

Hi Folks, my shell app is running well except for the small matter of when I delete a record it deletes the whole file.
if test $accept = "y"
   then
   sed /$title/cdfile > f
   cp f $cdfile
fi ;;

This is the code that kills my file.  I no doubt have something wrong that I have not picked up.
Many thanks for your help.
Regards Ned
0
Ned_Kelly
Asked:
Ned_Kelly
  • 6
  • 5
1 Solution
 
griesshCommented:
Ned

Are you sure you got that sed right?

sed -e '/$title/d' cdfile

should be more likely to delete the $title from the file.

======
Werner
0
 
griesshCommented:
sed -e "/$title/d" cdfile
0
 
Ned_KellyAuthor Commented:
Hi folks, thanks for your comments.  I have tried your suggestions and it still empties the file rather than the selected record.
No I am not sure if I got it right, being new to shell programming.  I noticed that you both have the -e and I'm not sure of this switch.  
It writes to a file music that I have linked with
cdfile=/common/tom/music

the code is now
sed -e '/$title/d' cdfile > music
cp music $cdfile

So am I replacing the old file with a new file that has nothing in it and over writing the old file.\
Thankyou for your help.
Regards Ned
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
MindphaserCommented:
... listening
0
 
griesshCommented:
Ned

The -e is the execute command for sed, the d in the expression stands for 'delete'.
You have to have 'title' set to something you try to delete. Do it manually first without replacement:

$export title=xyz
$sed -e "/$title/d" cdfile

The output goes to the console, you should see xyz missing.
If cdfile is not the filename, but a variable you have to use:

$sed -e "/$title/d" $cdfile

to get the CONTENT of the variable!
Your output file 'music' is not by any chance created in /common/tom? Then you would overwrite your
input ...

If your input file is: /common/tom/music
and you want to create a new /common/tom/music the way you do it, then

cd /common/tom
sed -e "/$title/d" music > temp
cp tmp music

If you want to use variables, you have to use them consistantly:
export cdfile=/common/tom/music
use "$cdfile" instead of "music".

======
Werner
0
 
Ned_KellyAuthor Commented:
Hi folks, thanks for your help.
This writes a new file temp to the tom dir but it then writes nothing back to music.  I want to delete one record and leave the rest intact.  Instead it deletes the lot.

#cdmenu
cdfile=-/common/tom/music
loop=y
while [ "$loop" = y ]
do
     clear
     cursor 3 12; echo "CD Collection"
     cursor 5 12; echo "V - View music file    : "
     cursor 5 12; echo "A - Add CD to list     : "
     cursor 5 12; echo "S - Search for title   : "
     cursor 6 12; echo "D - Delete a Record    : "
     cursor 7 12; echo "Q - Quit"
     cursor 10 32;
     read choice || continue
          case $choice in
               [Aa]) cdadd ;;
               [Vv]) cdlist ;;
               [Ss]) cdsearch ;;
               [Dd]) cursor 16 4; echo "Delete CD Record"
                     cursor 17 4; echo "Enter CD Title"
                     cursor 18 4; read title
                     cursor 19 4; echo "Accept? (Y)es or (N)o"
                     cursor read accept
                     if test $accept = "y"
                        then
                        sed -e "/$title/d" cdfile > tmp
                        cp tmp $cdfile
                     fi ;;
               [Qq]) exit ;;
               *) cursor 14 4; echo "Invalid code"; read prompt ;;
          esac
done

Many thanks for your help.
Ned
0
 
Ned_KellyAuthor Commented:
Quick note, title is the second value in the record ie:-

Artist   Title   Date    Cost

Sade:Love Delux:11-11-89:15.00

the data is stored in the file as above
Ned
0
 
griesshCommented:
Ned

Again: If you want to use variables, you have to use them everywhere:
sed -e "/$title/d" $cdfile > tmp

Just do it manually without the pipe and you will see the difference.

sed -e "/$title/d" cdfile
will use a nonexisting file "cdfile" and the result is an empty file! If you filter a file without content
you will get a file without content. You have to understand that "cdfile" is a filename in this context,
"$cdfile" is the content of the variable "cdfile".

======
Werner
0
 
griesshCommented:
Ned

Do the following:

Create a file "mymusic" with the content:
Sade
Stones
Beatles
Grand Funk Railroad

Run the following commands:
export myfile=mymusic
export mytitle=Stones

You can run the following commands:
echo myfile
echo $myfile
and you will see the difference between "myfile" and "$myfile".

sed -e "/$title/d" $myfile
sed -e "/$title/d" myfile

will show you the completely differnt outputs of sed.

======
Werner
0
 
Ned_KellyAuthor Commented:
Hi griessh, thankyou for the lesson.  I now go to beat my head against the wall.  Something so obvious and I miss it.  My Program now deletes the said record and leaves the file as is.  
May your days be filled with happiness and good fortune.
Again thankyou for the lesson.
Regards Ned
0
 
griesshCommented:
>> I now go to beat my head against the wall

Don't do that ;-)
Things like that happen all the time and that's why we are here. I think this thread is a wonderful example about the value of experts-exchange. Together we can make this work!

======
Werner
0
 
Ned_KellyAuthor Commented:
DITTO
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now