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!

3)
 echo
 echo "You have chose to Edit an Existing Entry"
 echo "Enter part or all of a name/phone# or address"
 echo 
 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
   name=$oldName
 fi
 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
   phone=$oldPhone
 fi
 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
   address=$oldAddress
 fi
 echo "${name}:${phone}:${address}" >> addressbook.txt
 ;;

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

- Jim

Recommended Answers

All 2 Replies

Ah silly me...

Forgot about sed.

Hey There,

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

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

to

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

should inline replace the file.

Hope that helps :)

, Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.