Solved

Delete a record in shell program.

Posted on 2002-06-11
13
292 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
  • 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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 25 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle query help 18 113
Installed softare without GUID 6 77
Definitions and default visual studio colors 5 65
Beginner to Unreal Engine 4 5 80
This article will show, step by step, how to integrate R code into a R Sweave document
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …

831 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