basically, what i am trying to do is delete a line from a file. The user enters a search string then the program searches for everything other than the search string then stores it in the file. Here is my code:
elif [ $res -eq "2" ]
then
echo "Enter phrase to delete: "
read -e deletestr
d=`cat phonebook | grep -v $deletestr`
echo $d > phonebook
When I run the script it always empties the phonebook file. Why is this?
I’d suggest either creating a new file, and replacing the old file after you’ve done the removal, or using a tool such as
sed -ithat can edit a file in place.Something like:
Or