Solved

Delete a record in shell program.

Posted on 2002-06-11
13
262 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
Comment Utility
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
Comment Utility
sed -e "/$title/d" cdfile
0
 

Author Comment

by:Ned_Kelly
Comment Utility
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
 
LVL 6

Expert Comment

by:Mindphaser
Comment Utility
... listening
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:Ned_Kelly
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
DITTO
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now