Solved

Delete a record in shell program.

Posted on 2002-06-11
13
285 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 57
Arduino EDI - Programming language 3 80
Adoquery sql  left join does not work 25 81
simplest php form 3 64
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

863 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

27 Experts available now in Live!

Get 1:1 Help Now