ok, right to the point, here are the codes, I formatted the codes a bit 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=/[az]/ ; print "$0=/[az]/ ; value of $0 is ",$0 $0=t ; $0=/[0-9][az]/ ; print "$0=/[0-9][az]/ ; 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 conclusion:
$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=/[az]/ ; value of $0 is 1 $0=/[0-9][az]/ ; 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 do not understand why I got this result. I thought awk would complain about the assignment operator, but it doesn't ... awk seems to do a weird regex match when I did $0=/xxx/
? i.e. $0=/pattern/
matches $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 foo=/pattern/
matches foo=$0~/pattern/
but I'm not sure ... I can not find the information in the document / man page.
I found it when I answered the awk question here @SO.
I tested 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 anyone can explain to me. Thanks in advance.
EDIT
after performing more tests, it turns out (again, not sure, since I did not find it in the document), inside {...}
, only /pattern/
is a short form $0~/pattern/
the same as outside {...}
, for example /patter/{do something}
. so we could do:
kent$ echo "xx"|awk '{if(/x/)print "ok"}' ok
I don't know if this is a standard awk script syntax function. and if it works for the whole awk implementation. I'm still looking for a man page and man page ...