Good afternoon all!

I am writing an address book for a class that I have, and am stuck on this last part. Any help would be greatly appreciated!

When the user selects the "Edit existing record" option, I can't figure out how to make the file overwrite the previous record. As it stands now, it makes a new record with the new data, leaving the old record intact.

Currently written in bash.

Thank you in advance for your assistance!

 echo "You have chose to Edit an Existing Entry"
 echo "Enter part or all of a name/phone# or address"
 read search
 choiceLine=`grep -i "$search" addressbook.txt`
 oldName=`echo $choiceLine|cut -d ":" -f1`
 oldPhone=`echo $choiceLine|cut -d ":" -f2`
 oldAddress=`echo $choiceLine|cut -d ":" -f3`
 echo "You have chosen to search for : $search"
 grep -v "$search" addressbook.txt > addressbook.tmp
 echo "If you don't wish to change the name, press enter"
 echo -n "The name [ $oldName ] will be changed to: "
 read name
 if [ -z "$name" ]; then
 echo "The previous phone number was: $oldPhone"
 echo "If you don't wish to change the number, press enter"
 echo -en "Please enter a new phone number: "
 read phone
 if [ -z "$phone" ]; then
 echo "The previous address was: $oldAddress"
 echo "If you don't wish to change the address, press enter"
 echo -en "Please enter a new address: "
 read address
 if [ -z "$address" ]; then
 echo "${name}:${phone}:${address}" >> addressbook.txt

Again, thank you for taking the time to point out my error(s)

- Jim

Hey There,

Also, at the end of your code if you change:

echo "${name}:${phone}:${address}" >> addressbook.txt


echo "${name}:${phone}:${address}" > addressbook.txt

should inline replace the file.

Hope that helps :)

, Mike

This article has been dead for over six months. Start a new discussion instead.