Required Explanation of the awk Variable

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 ...

+4
source share
2 answers

/regexp/ is just a shorthand for $0 ~ /regexp/

var = ($0 ~ /regexp/) assigns var the result of the comparison, 1 for true, zero otherwise.

$0 = ($0 ~ /regexp/) assigns $ 0 the result of comparing $ 0 and / regexp /

$0 = ($0 ~ //) assigns $ 0 the result of searching for an empty string in $ 0, which always exists, therefore the condition is true, therefore the result is 1

$0 = // does the same

$0 = /*/ looks for the literal * in $ 0, which is not the case, that the result is zero.

$0 = /**/ looks for a literal * repeated ZERO or more than $ 0 times, of which they are zero, so the result is 1.

No secret, nothing strange ...

+6
source

There is no secret here, you just set the value $0 to the value of the estimated regular expression match. First, the right-hand side is evaluated if the string matches the regular expression, the result is True, that is 1, otherwise the results will be False, i.e. 0.

+2
source

Source: https://habr.com/ru/post/1502122/


All Articles