Hey, thanks for all the help gang! I apologize for the homework thing; I must have skimmed that part a little too quickly. In my defense, I was trying to be as specific-to-the-exact-issue-at-hand as possible, so as to not let any assistance I might get spread outside that sphere of influence.
Try saying *that* three times fast!
Anyhoo, I did some googling and, in conjunction with your ideas, got a while-read loop going that works - mostly.
Here's some internal information that should be helpful, before I get to the actual issue/s - some of this was in my initial post, but this way we don't need to worry about scrolling and the like
:
When the user stores an entry, the 4 fields (Name (might change to first and last names later, but who knows), Address, Phone Number, and Email) are appended to a straight text file called address.book with carets ("^") as delimiters. So, for example, you might see something like:
John Doe^123 Main St^555-1234^jdoe@server.com
if you were to cat the address.book file. When the file is searched, as I'm sure you guys could guess, no reformatting is necessary 'cos grep will either find a match or not regardless of formatting. When an entry is printed, I've got an awk statement to print each line out and to filter out the carets and add newlines in their steads.
I've been working, as I think I mentioned, on a delete function. As I said above, it works for the most part. The only times that it hiccups are:
If there are two identical entries containing letters (e.g. if I throw in the test entry "a^b^c^d", or the full version listed above), it deletes both of them. This is fine; I understand exactly why this happens and I think I've got a way around it that'd involve using the -n flag with grep. Not sure how to implement it just yet, but I'm sure I'll come up with something.
The other time it has issues is if there's two identical entries containing *just* numbers (e.g. "1^2^3^4") in which case it deletes NEITHER of them, but goes through the loop and - as far as I can tell - honestly truly believes that it HAS in fact deleted them.
I have no bloody clue why it's doing this, except maybe to punish me for being too ambitious.
The full delete loop looks like this:
Code:
# Function to delete entries
# Checks to see if the pattern exists in the file. If not, advises user. Else, regardlessof number of hits, cycles through each hit and confirms deletion with user.
deleteEntry() {
DELETECHECK=0
FILENAME=address.book
echo "Please enter some part of the entry you would like to delete (e.g. name, email, etc):"
read DELETEME
exec 3<"$FILENAME" #reads address.book through not-stdin so that the user's search pattern can be read with stdin.
while read -u 3 LINE
do
if [ `echo $LINE | grep -i $DELETEME | wc -l` -gt 0 ] #if there's at least 1 instance of the pattern in the line
then
echo $LINE | awk -F^ '{print "\n", " ---------- ", "\n", "\n", $1, "\n", $2, "\n", $3, "\n", $4, "\n"}' # print statement used throughout
echo "Delete this entry? (y/n)"
read CONFIRM
if [ $CONFIRM == y -o $CONFIRM == Y ]
then
cat address.book | grep -vi "$LINE" > address.book1
mv address.book1 address.book
fi
DELETECHECK=1
fi
done
exec 3<&-
if [ $DELETECHECK -eq 1 ]
then
echo "Deletion occurred. Here is what your address book looks like:"
awk -F^ '{print "\n", " ---------- ", "\n", "\n", $1, "\n", $2, "\n", $3, "\n", $4}' address.book
else
echo "Your search pattern wasn't found in the address book. No changes have been made."
fi
}
Is there something blindingly obvious that I'm just missing? I'm thinking maybe it's treating the 1/2/3/4 as variables instead of as straight-up text, in which case I should be able to sneak past that with some clever quote-mark usage....right?
Otherwise, if you see anywhere to optimize the code a little more, I'd certainly appreciate the advice! (I'm pretty sure I don't need to put the file name in a variable for the "read the file with not-stdin" part, but I'm not 100% there, and it mostly works as is...)