需要awk变量赋值语句说明 [英] awk variable assignment statement explanation needed

查看:92
本文介绍了需要awk变量赋值语句说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,直截了当,这是代码,我对代码进行了一些格式化以使其易于阅读:

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"

我得到了这个输出:

$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

我不明白为什么得到此输出.我以为awk会抱怨赋值语句,但事实并非如此……当我执行$0=/xxx/时,awk似乎正在执行奇怪的正则表达式匹配检查?即$0=/pattern/是否与$0=$0~/pattern/相同?

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/?

然后我做了这个测试:

kent$  echo "xx"|awk '{y="777";y=/x/;print y}'                                                                                                                              
1

kent$  echo "xx"|awk '{y="777";y=/7/;print y}'                                                                                                                              
0

所以看起来foo=/pattern/foo=$0~/pattern/

但是我不确定...在文档/手册页中找不到该信息.

but I am not sure... cannot find the info in document/man page.

当我在这里回答awk问题时,我发现了它.

I found it when I was answering an awk question here @ SO.

我用awk进行了测试:

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.

编辑

经过更多测试后,结果再次出现(不确定,因为我没有在文档中找到它),之内{...},仅/pattern/$0~/pattern/{...}外部相同,例如/patter/{do something}.这样我们就可以做到:

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

我不知道它是否是awk脚本语法的标准功能.以及它是否适用于所有awk实施.我仍在搜索手册和手册页...

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

推荐答案

/regexp/只是$0 ~ /regexp/

var = ($0 ~ /regexp/)将var的比较结果分配给var,比较结果为1,否则为0.

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

$0 = ($0 ~ /regexp/)为$ 0分配$ 0和/regexp/

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

$0 = ($0 ~ //)向$ 0分配在$ 0中查找空字符串的结果,该字符串始终存在,因此条件为true,结果为1

$0 = ($0 ~ //) assigns $0 the result of looking for an empty string in $0, which there always is, so the condition is true so the result is 1

$0 = //相同

$0 = /*/正在$ 0中查找文字*,没有,因此结果为零.

$0 = /*/ is looking for a literal * in $0, which there isn't so the result is zero.

$0 = /**/正在寻找在$ 0中重复零次或多次的立即数*,它们为零,因此结果为1.

$0 = /**/ is looking for a literal * repeated ZERO or more times in $0, which there are zero of them so that result is 1.

没有神秘,没有奇怪...

No mystery, nothing weird...

这篇关于需要awk变量赋值语句说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆