ok, straight to the point, here is the codes, I formatted the codes a little to make it easy to read:
awk '{
t=$0 ;
$0=t ; $0=// ; print "$0=// ; value of $0 is ",$0
$0=t ; $0=/./ ; print "$0=/./ ; value of $0 is ",$0
$0=t ; $0=/*/ ; print "$0=/*/ ; value of $0 is ",$0
$0=t ; $0=/**/ ; print "$0=/**/ ; value of $0 is ",$0
$0=t ; $0=/[0-9]/ ; print "$0=/[0-9]/ ; value of $0 is ",$0
$0=t ; $0=/[a-z]/ ; print "$0=/[a-z]/ ; value of $0 is ",$0
$0=t ; $0=/[0-9][a-z]/ ; print "$0=/[0-9][a-z]/ ; value of $0 is ",$0
$0=t ; $0=/5/ ; print "$0=/5/ ; value of $0 is ",$0
$0=t ; $0=/55/ ; print "$0=/55/ ; value of $0 is ",$0
$0=t ; $0=/x/ ; print "$0=/x/ ; value of $0 is ",$0
$0=t ; $0=/5x/ ; print "$0=/5x/ ; value of $0 is ",$0
$0=t ; $0=/x5/ ; print "$0=/x5/ ; value of $0 is ",$0
$0=t ; $0=/xoo/ ; print "$0=/xoo/ ; value of $0 is ",$0
$0=t ; $0=/500/ ; print "$0=/500/ ; value of $0 is ",$0
}'<<<"5x"
I got this output:
$0=// ; value of $0 is 1
$0=/./ ; value of $0 is 1
$0=/*/ ; value of $0 is 0
$0=/**/ ; value of $0 is 1
$0=/[0-9]/ ; value of $0 is 1
$0=/[a-z]/ ; value of $0 is 1
$0=/[0-9][a-z]/ ; value of $0 is 1
$0=/5/ ; value of $0 is 1
$0=/55/ ; value of $0 is 0
$0=/x/ ; value of $0 is 1
$0=/5x/ ; value of $0 is 1
$0=/x5/ ; value of $0 is 0
$0=/xoo/ ; value of $0 is 0
$0=/500/ ; value of $0 is 0
I cannot understand why I got this output. I thought awk would complain about the assignment statement, but it didn't... It seems that awk is doing weird regex matching check when I did $0=/xxx/
? that is, $0=/pattern/
is same as $0=$0~/pattern/
?
then I did this test:
kent$ echo "xx"|awk '{y="777";y=/x/;print y}'
1
kent$ echo "xx"|awk '{y="777";y=/7/;print y}'
0
so it seems, foo=/pattern/
is same as foo=$0~/pattern/
but I am not sure... cannot find the info in document/man page.
I found it when I was answering an awk question here @ SO.
I tested with my awk:
kent$ awk --version|head -1
GNU Awk 4.1.0, API: 1.0 (GNU MPFR 3.1.2, GNU MP 5.1.2)
I appreciate if someone could explain me. Thanks in advance.
EDIT
after doing more tests, it turns out (again, not sure, since I didn't find it in document), within {...}
, the /pattern/
alone is short form of $0~/pattern/
same as outside the {...}
, like /patter/{do something}
. so we could do:
kent$ echo "xx"|awk '{if(/x/)print "ok"}'
ok
I don't know if it is a standard feature of awk script syntax. and if it works for all awk implementation. I am still searching manual and man page...