Reputation: 27
I'm trying to write a function that looks for a last name (from user input) in the second column and returns the line to the user. Here is what I've tried (and is not working). Most importantly, when I run this with the last name in place of $input_name at the command line, it works then. As you can see, the echo to confirm that the user input was read properly. What am I missing?
60 last_search () {
61 echo "What last name are you looking for?"
62 read input_name
63 echo "$input_name"
64 awk -F':' '$2 ~ /$input_name/{print $0}' temp_phonebook
65 }
Upvotes: 0
Views: 840
Reputation: 27
okay, I figured it out! I need single quotes around the variable '$input_name'
Upvotes: -1
Reputation: 20909
Your awk command is in single ticks, so $input_name
never gets expanded.
Try something like this:
awk -F':' '$2 ~ /'"$input_name"'/{print $0}' temp_phonebook
Alternately, supply the value in advance using -v name=value
:
awk -F':' -v search="${input_name}" '$2 ~ search {print $0}' temp_phonebook
Upvotes: -1
Reputation: 785876
-v name=value
to pass a shell variable to awkprint $0
is default action so take it out.You can use:
awk -F: -v input_name="$input_name" '$2 ~ input_name' temp_phonebook
Upvotes: 4