Awk partial match is not printed

awk -F"," -v var=$test '$1 ~ /^var$/{print}' alpha.txt

      

I tried hard-coding my var with my actual variable input and I found that this code works. However, when I tried, for example, to /^ppl$/

find a partial match with an apple, it doesn't show up. can anyone give me some guidance as to how I can parse my variable in a command?

+3


source to share


3 answers


try this:



awk -F"," -v var=$test '$1 ~ "^"var"$"' alpha.txt

      

+2


source


awk -F"," '$1 ~ /^'"$test"'$/{print}' alpha.txt

      



0


source


If you are binding the match at the beginning and end, just use ==

awk -F"," -v var=$test '$1 == var' alpha.txt

      

If $test

contains a regex, then @Kent has the correct answer.

0


source







All Articles