user1823986
user1823986

Reputation: 87

awk partial matching not being 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 /^ppl$/ to search for partial match of apple, it does not display. can someone give me some guidance as to how I can parse my variable into the command?

Upvotes: 3

Views: 961

Answers (3)

glenn jackman
glenn jackman

Reputation: 246754

If you're anchoring the match at the beginning and the end, just use ==

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

Unless $test contains a regular expression, in which case @Kent has the right answer.

Upvotes: 0

Kent
Kent

Reputation: 195039

try this:

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

Upvotes: 2

Michael J. Barber
Michael J. Barber

Reputation: 25032

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

Upvotes: 0

Related Questions