Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Delete a record in shell program.

Posted on 2002-06-11
13
Medium Priority
?
321 Views
Last Modified: 2010-04-17
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
Comment
Question by:Ned_Kelly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
13 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 7071780
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
 
LVL 11

Expert Comment

by:griessh
ID: 7072588
sed -e "/$title/d" cdfile
0
 

Author Comment

by:Ned_Kelly
ID: 7072930
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Expert Comment

by:Mindphaser
ID: 7073121
... listening
0
 
LVL 11

Expert Comment

by:griessh
ID: 7073124
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
 

Author Comment

by:Ned_Kelly
ID: 7073214
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
 

Author Comment

by:Ned_Kelly
ID: 7073229
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
 
LVL 11

Expert Comment

by:griessh
ID: 7073598
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
 
LVL 11

Accepted Solution

by:
griessh earned 100 total points
ID: 7073616
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
 

Author Comment

by:Ned_Kelly
ID: 7075416
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
 
LVL 11

Expert Comment

by:griessh
ID: 7075438
>> 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
 

Author Comment

by:Ned_Kelly
ID: 7075447
DITTO
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Simple Linear Regression
Starting up a Project

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question