awk variables in regex expression ?

Hello,

Could someone explain why this one returns nothing:

$ x=/jon/
$ echo jon | awk -v xa=$x '$1~xa {print}'
$

while the following works fine:

$ x=jon
$ echo jon | awk -v xa=$x '$1==xa {print}'
$ jon

and the following works fine:

$ echo jon | awk '$1~/jon/ {print}'
$ jon

using bash.

thanks
V

When a pattern matching is done using ~ we need to give the reg-exp in between / / (2 forward slashes).Still, when given inside the slashes it is taken as literally, so when changing the above awk as

echo jon | awk -v xa=$x '$1~/xa/ {print}'

it matches the word xa and not the variable xa's value.Hence we need to expand the variable as below

x=/jon/
echo jon | awk -v xa=$x '$1~/'"$xa"'/ {print}'

This is not the case when == is used as you would know this is used to equate between variables.Therefore in code $1==xa.. xa is expanded (to jon).

thanks that helped very much!

I have a question about something similar.

awk ' 
BEGIN { split(" ' " ` date ` " ' ", date) } 
$1 == date[2] && $2 == date[3] {print $0}
' $1 

Why are there two pairs of quotes? I would say that there is one pair of double quotes too many, but when I remove them, it produces an error (different, depending on which pair I remove).

P.S. The script is meant to be a calendar reminder. There is an additional text file. It contains something like the following text:

Sep 30  mother's birthday
Oct  1  lunch with joe, noon
Oct  1  meeting 4pm