Variables within awk

Hi,

I'm having trouble getting awk to read a variable with spaces in it.

Input:
vendorName="Bob's Steakhouse"
awk -F":" '$2 ~ /'$vendorName'/ {print $1}' Purchases.dat

Error:
awk: $2 ~ /Bob's
awk:       ^ unterminated regexp

The awk command isn't recognizing the entire string. It points to the first letter of the first string in the variable. How can I make it so awk recognizes the entire variable?

Also, I'm not entirely sure if a single quote screws up the awk command either.

Thanks

FYI: The caret symbol is supposed to be pointing to the capital B.

Well...

  1. Shell and awk are different utilities and they have different variables. Shell variables and awk variables are not the same things.
  2. You can transfer values of shell variables to awk variables or awk expressions.
  3. What you try to do - transfer values of shell variable to awk expression. (You can see what happens with 'set -x'). Shell variables expanded before awk get its program and you have this command:
awk -F":" '$2 ~ /'Bob\'s Steakhouse'/ {print $1}' Purchases.dat

awk should get its program as one piece but it gets four:

'$2 ~ /'
Bob\'s
Steakhouse
'/ {print $1}'

This all is a consequence of the shell syntax quoting rules.
4. Ok, what can you do. The universal but unsafe and often cumbersome method is to put your shell variables in double quotes:

awk -F":" '$2 ~ /'"$vendorName"'/ {print $1}' Purchases.dat

Shell expand the variable but stop here. You get:

awk -F: '$2 ~ /'"Bob's Steakhouse"'/ {print $1}'

And once shell see several different adjacent quoted strings it glues them in one so the final result is something like this:

awk -F: '$2 ~ /Bob\'s Steakhouse/ {print $1}'
  1. This method works for any command utilities, like sed, perl, sql clients, etc. But awk has much better (less cumbersome and much safe) method (or two to be exact). You can transfer value of shell variables to awk variables in command line arguments. The first one is with -v option:
vendorName="Bob's Steakhouse"
awk -F":"  -vvendor="$vendorName" '$2 ~ vendor {print $1}' Purchases.dat

The second one: use the assignment among file arguments:
vendorName="Bob's Steakhouse"
awk -F":" '$2 ~ vendor {print $1}' vendor="$vendorName" Purchases.dat

Hope it may help. Sorry for my English (this post really is my daily English exercise :slight_smile: ).

1 Like

Your last method at the very bottom proved to be useful. Thank you very much for your help :smiley: