Awk script problem - Variables Causing Issue

can someone please explain to me what i'm doing wrong with this code:

WELT=$(awk '(($1 ~ "^${caag}$") || ($2 ~ "^${caag}$"))' /tmp/Compare.TEXT)

when run from the command line, it works. but it seems to be having a problem doing the comparison when variables are involved.

i tested from the command line and assigned a value to "caag", it wouldn't work.

but when i just replaced caag on the command line with the actual value, it passes.

i've been stalled all day by this. any help will be much appreciated.

Variables do not expand inside single quotes:

$ echo '${HOME}'
${HOME}

$

Awk has better means to pass variables than embedding them in the string, fortunately:

WELT=$(awk -v CAAG="^${caag}\$" '(($1 ~ CAAG) || ($2 ~ CAAG))' /tmp/Compare.TEXT)

If awk complains about this syntax, try nawk. Some systems keep a very old awk around for compatibility reasons.

1 Like