Chris
Chris

Reputation: 1219

Awk match column using variable and word boundaries

I am trying to print lines where the first column contains a match with "var" using boundaries.

awk -F '|' '$1 ~ /\<$var\>/ { print $0 }'

It works as expected without trying to use a variable.

echo "18,19,20|hello world" |  awk -F '|' '$1 ~ /\<18\>/ { print $0 }'
18,19,20|hello world

How can I do it using my variable?

Upvotes: 0

Views: 91

Answers (1)

Ed Morton
Ed Morton

Reputation: 203522

awk -F '|' -v var="$var" '$1 ~ ("\\<"var"\\>")'

See https://www.gnu.org/software/gawk/manual/gawk.html#Computed-Regexps

Upvotes: 2

Related Questions