Reputation:
I have a script that removes Contacts from a csv file.
Csv file is like: First Name,Last Name,Phone Number
Example: John,Doe,2109428409
Script asks user to input Last Name and whole line gets erased. If there's a duplicate last name in the csv file how can I ask the user first name of one of the two contact he wants to delete? Here's my code
#!/bin/bash
echo "Tell Me The Contact's Last Name you want to delete"
read LastName
if grep -q $LastName contacts.csv
then
sed -i '/'$LastName'/d' contacts.csv
else
echo "The surname you typed doesn't exist or isn't valid"
fi
Upvotes: 0
Views: 307
Reputation: 1319
We can add something like below, to your script,
#!/bin/bash
echo "Tell Me The Contact's Last Name you want to delete"
read LastName
# --------------------------------------------------------#
if [ $(grep $LastName contacts.csv | wc -l) -gt 1 ];then # --> check the count of Last name in file
echo "enter the first name from below list of contact you want to remove"
grep $LastName contacts.csv # --> Display list of entries with same Last Name
read firstName # --> Read First name Input from user
sed -i '/'$firstName,$LastName'/d' contacts.csv # --> delete row with first name and Last Name.
elif [ $(grep $LastName contacts.csv | wc -l) -eq 1 ];then
# --------------------------------------------------------#
sed -i '/'$LastName'/d' contacts.csv
else
echo "The surname you typed doesn't exist or isn't valid"
fi
Upvotes: 2