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
sort -u -o datFile datFile. Good luck. - shelltergrep -coutputs the number of matching lines, so instead of testing whether we have a match, you ask for the number of matches and consider 3 cases: 0 matches, 1 match, more than one match. However, your script has other problems: (1) You are testing for the occurance of $LastName in any place of the CSV file, not only in the column for last names. (2) A malevolent user could enter for what you take as his last name, a regexp pattern which wipes out your whole CSV file. By and large, this is not a problem I personally would solve in Posix shell. - user1934428$LastNamein a specific field, not just anywhere in the line. That makesgrepinappropriate for this task. - chepner