Searching a file - and assigning responses to variables (or something)

So first: Sorry if the title is confusing...

I have a script I'm writing with a file with several names in it (some other info - but it's not really pertinent...) - I want to be allow the user to delete certain records, but I ran into a problem I'm not sure how to go about fixing.

If I were doing this in Java - I would simply say 'record 1 = so-and-so' and 'record 2 = this other guy.' Which one do you want to delete? But how can I do that in a bash script?

A better example:

Who do you want to delete?  Nikki

Found 2 Nikki's:
1 - Nikki the Teacher
2 - Nikki the Secretary

Which one to delete?

I am searching the file using nawk '$name' - any suggestions on having the script say 'here's 1 - that's #1, here's another - that's #2 - which one do you want?'

Thanks...

What operating system are you using?

Please show us what code you have tried to solve this problem (in CODE tags).

Can you apply anything from what you learned in your previous thread to solving this problem?

It's Ubuntu - and as for code, there's actually not much at the moment. I've been toying with my other post's code trying to figure something out; but wasn't having much luck.

echo -n "Enter Name to find: "
read name
nawk -F: 'BEGIN{IGNORECASE = 1} /'"$name"'/ {printf "Record found - %s\n", $1}' data

As you can see, it's searches the file by $name and prints out what it found. I've tried a counter in the nawk statement - but that kept coming up with 0 - even when it showed 2 different records found.

And that's where I'm stuck. If I didn't mention it above, this is a bash script.

Thanks for your reply - and sorry about not using code above.

How about

read -p"Who do you want to delete? " NAME
Who do you want to delete? Nikki
select DN in $(grep "$NAME" file) quit; do echo "$DN"; [ "$DN" = "quit" ] && break; done
1) Nikki the Secretary
2) Nikki the Teacher
3) quit

RudiC - that's almost perfect (and Thanks!) - except, for me it's doing this:

1) Nikki
2) the
3) Secretary

But that's very close to what I was looking for. I'm playing around with it to see if I can make this work... I'll keep at it :slight_smile:

Sorry - I forgot to post that I set the

IFS="
"

(<newline> char) upfront to avoid exactly the problem that you encountered...

1 Like

Thanks for the help guys - I got everything polished up and working great.