[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: shell programming question



Keith wrote:
  >I was wondering if someone could help me with this shell programming
  >language question. I am trying to remove some entries from a database
  >file.
  >Here is what the database file looks like:
  >
  >Joe,Walsh,134 Mockingbird Way,New Orleans,LA,33456,(444)768-1287
  >Becky,Walsh,432 Rodeo Drive Apt C,Hollywood,CA,90312,(555)555-5555
  >
  >Here is the function that I am using to remove one of the entries.
  >Instead of removing only one of the entries it removes both. It does 
  >this because I am removing the entries by lastname. SO if more than
  >one person has the same last name they will all be deleted instead
  >of just the one I want. I need to know if there is a way to modify
  >this script to delete entries by first and last name, not just the
  >last name.
  >
  >Here is the script.
  >
  >remove_records() {
  >  if [ -z "$lastname" ]; then
  >    echo You must select an address first
  >    find_ad n
  >  fi
  >  if [ -n "$lastname" ]; then
  >    echo "You are about to delete $firstname $lastname"
  >    get_confirm && {
  >      egrep -f "$firstname" "$lastname" $address_file > $temp_file
  >      mv $temp_file $address_file
  >      lastname=""
  >      echo Entry removed
  >    }
  >    get_return
  >  fi
  >  return
  >}
  
Why `egrep -f $firstname' ?

       -f FILE, --file=FILE
              Obtain patterns from FILE, one per line.  The empty
              file contains zero patterns, and  therfore  matches
              nothing.

Presumably you need to test for $firstname as well, if you need to 
use it to distinguish entries.

Altered lines are marked thus:  <--

remove_records() {
  if [ -z "$lastname" ]; then
    echo You must select an address first
    find_ad n
  fi
  if [ -n "$lastname" -a -n "$firstname" ]; then                 <--
    echo "You are about to delete $firstname $lastname"
    get_confirm && {
      grep -v "^$firstname,$lastname," $address_file > $temp_file    <--
      mv $temp_file $address_file
      lastname=""
      echo Entry removed
    }
    get_return
  fi
  return
}
-- 
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
               PGP key from public servers; key ID 32B8FAA1
                 ========================================
     "But the wisdom that is from above is first pure, then 
      peaceable, gentle, and easy to be intreated, full of 
      mercy and good fruits, without partiality, and without
      hypocrisy."     James 3:17 



--  
Unsubscribe?  mail -s unsubscribe debian-user-request@lists.debian.org < /dev/null


Reply to: