Click to See Complete Forum and Search --> : variables in awk pattern


CyberCoder
06-03-2005, 11:38 AM
How to insert variables in awk patterns? Say I have variable text and want to test if it matches the current field, I could write something like this:
awk -v text="abc" '/text/ {print "matches"}'
But the field would be matched with "text", not with "abc".

CyberCoder
06-03-2005, 03:26 PM
Oops... I didn't get the point about patterns. This code
awk -v text="abc" '$0 ~ text {print "matches"}'
seems to be the correct one.

CaptainPinko
06-03-2005, 04:44 PM
Originally posted by CyberCoder
Oops... I didn't get the point about patterns. This code
awk -v text="abc" '$0 ~ text {print "matches"}'
seems to be the correct one.

well if text is a shell variable then

text="abc";
awk -v '$0 ~ '$text' {print "matches"}'

should work, though I don't use awk so I might have misread your question.