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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching a file - and assigning responses to variables (or something)
# 1  
Old 05-10-2016
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:

Code:
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...

Last edited by Don Cragun; 05-10-2016 at 02:16 AM.. Reason: Add CODE and ICODE tags
# 2  
Old 05-10-2016
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?
# 3  
Old 05-10-2016
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.

Code:
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.
# 4  
Old 05-10-2016
How about
Code:
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

# 5  
Old 05-10-2016
RudiC - that's almost perfect (and Thanks!) - except, for me it's doing this:

Code:
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 Smilie
# 6  
Old 05-10-2016
Sorry - I forgot to post that I set the
Code:
IFS="
"

(<newline> char) upfront to avoid exactly the problem that you encountered...
This User Gave Thanks to RudiC For This Post:
# 7  
Old 05-11-2016
Thanks for the help guys - I got everything polished up and working great.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning Variables

Hi, Can the below be clarified please. i just want to know what is the difference between the two ways of assigning variables as mentioned below. export SRC_TBL=${SRC_TBL-"MMA_COPAY_PLN_FACT_STG"} export SRC_TBL="MMA_COPAY_PLN_FACT_STG" thanks in advance :) Arun (1 Reply)
Discussion started by: Arun Mishra
1 Replies

2. Shell Programming and Scripting

Assigning variables

i have variables RECIPIENTS_DEVL,RECIPIENTS_UACC,RECIPIENTS_PROD i have a case statement to get the phase variable: case ${WMD_UPHASE1} in u) WMD_UPHASE4=UACC;; i) WMD_UPHASE4=DEVL;; p) WMD_UPHASE4=PROD;; d) WMD_UPHASE4=DEVL;; *) WMD_UPHASE4=DEVL;; esac I am unable to... (3 Replies)
Discussion started by: Arun Mishra
3 Replies

3. Shell Programming and Scripting

Searching for file names with variables

Hello everyone We have a problem about searching and copying files with variables. we have variables like $year $jday $date and we want to search the files whose name contain these variables. we tried *$year*$jday*$date or with ? instead of * thank you everyone!!! (4 Replies)
Discussion started by: miriammiriam
4 Replies

4. Shell Programming and Scripting

Bash: Reading a file and assigning variables from file

I have a file that has four values on each line and I'd like to give each column a variable name and then use those values in each step of a loop. In bash, I believe you could use a while loop to do this or possibly a cat command, but I am super new to programming and I'm having trouble decoding... (2 Replies)
Discussion started by: ccorder22
2 Replies

5. Shell Programming and Scripting

Getting value from one file and assigning it to variables of another file.

Hi, I need to read two values,startdate and enddate from a param file and then write this value to another param file(global) under a specific workflow name.Here in global param file there is may workflow names, hence we need to check for the right one and then add the data below it. Any... (5 Replies)
Discussion started by: angel12345
5 Replies

6. Shell Programming and Scripting

Help in assigning values to variables from the file

Hi! This might be a simple thing, but I'm struggling to assign values to variables from the file. I've the following values stored in the file.. It consists of only two rows.. 10 20 I want to assign the first row value to variable "n1" and the second row value to variable "n2".. That is ... (3 Replies)
Discussion started by: abk07
3 Replies

7. Shell Programming and Scripting

Perl Help - Assigning variables to text file contents

I am looking to create a perl script which will take numbers from a simple text file, convert them from decimal to hex, and then rewrite those values in the file or create a new file with the hex numbers(whichever's easier). My text document for example would be something as simple as 1312... (6 Replies)
Discussion started by: samh785
6 Replies

8. UNIX for Dummies Questions & Answers

File Field Replacement, Assigning Fields to Variables, Lists/Arrays?

Okay, I've made threads on extracting fields and comparing strings in separate files in .csv's. I've written the following code with intentions of learning more. I just want this one question answered: How can I assign fields from a file(comma separated) to variables? My goal is to check... (0 Replies)
Discussion started by: chickeneaterguy
0 Replies

9. UNIX for Dummies Questions & Answers

Searching a text file and assigning it to a variable

Hi Gurus, I am new to unix.I have a requirement as below I have text file like a.txt which contains a.txt hi hello process update status Ok to Proceed no issues good data arrangement My requirement here is i need to read the file and check for the words "OK to Proceed" and if... (2 Replies)
Discussion started by: pssandeep
2 Replies

10. Shell Programming and Scripting

Assigning file attributes to variables

Hi, I'm trying to assign the permissions, owner and group of a file to seperate variables, but using ls -l filename | awk '{print $1 "\t" $3 "\t" $4}' gives the owner as tom.ja instead of tom.james Is there any way to expand it so i get the full name, or is there an easier way to get them... (5 Replies)
Discussion started by: olimiles
5 Replies
Login or Register to Ask a Question