简体   繁体   English

当awk中的模式规则不正确时执行的命令,怎么了?

[英]a command executed when pattern rule is not true in awk, what's wrong?

A strange awk phenomenon again.. (this happens all the time :) ) 再次出现一个奇怪的awk现象..(这一直在发生:))
I have a file aaa below. 我在下面有一个文件aaa。

[first]
aaa
bbb
[second]
ccc
ddd
eee
[third]
fff
ggg
hhh
iii

I'm trying to print the line numbers of each section which starts with a header embraced in bracket. 我正在尝试打印每个部分的行号,该行号以包含在括号中的标题开头。 So I wrote a simple awk script CntSecLines.awk below(it's being debugged, so with some prints). 因此,我在下面编写了一个简单的awk脚本CntSecLines.awk(正在调试,因此带有一些打印件)。

 /\[/{print "header found : "; print $keep " : " cnt; keep=$1; cnt=0}
!/\[/{print "header not found"; cnt = cnt+1; print "keep = " $keep;}

Below is the exection result for aaa. 以下是aaa的执行结果。

ckim@stph45:~/test] awk -f CntSecLines.awk aaa
header found : 
[first] : 
header not found
keep = aaa
header not found
keep = bbb
header found : 
[second] : 2
header not found
keep = ccc
header not found
keep = ddd
header not found
keep = eee
header found : 
[third] : 3
header not found
keep = fff
header not found
keep = ggg
header not found
keep = hhh
header not found
keep = iii

I intended it to be updated only when there is the section header. 我打算仅在有节标题时才对其进行更新。 But why is the variable 'keep' updated every line? 但是为什么变量“ keep”每行都会更新? when I print the variable $keep, we can see it's being updated every non-section-header line. 当我打印变量$ keep时,我们可以看到它正在每条非节头行中进行更新。

It's awk , not bash . awk ,不是bash In awk you don't use $ to get variable value (well, with exceptions, like for positional arguments $1 , $2 ...). awk您不使用$来获取变量值(当然,有例外,例如位置参数$1$2 ...)。

awk '
   /\[/{print "header found : "; print keep " : " cnt; keep=$1; cnt=0}
   !/\[/{print "header not found"; cnt = cnt+1; print "keep = " keep;}
' <<EOF
[first]
aaa
bbb
[second]
ccc
ddd
eee
[third]
fff
ggg
hhh
iii
EOF

will output: 将输出:

header found : 
 : 
header not found
keep = [first]
header not found
keep = [first]
header found : 
[first] : 2
header not found
keep = [second]
header not found
keep = [second]
header not found
keep = [second]
header found : 
[second] : 3
header not found
keep = [third]
header not found
keep = [third]
header not found
keep = [third]
header not found
keep = [third]

Tested on repl . repl测试。

I believe awk interprets $keep as, like, $ with a string [blabla] , like $"[blabla]" , then "[blabla]" is converted from a string to a number, which results in 0 , so $keep is interpreted as $0 , which prints the whole line. 我相信awk会将$keep解释为像$的字符串[blabla] ,例如$"[blabla]" ,然后将"[blabla]"从字符串转换为数字,结果为0 ,因此$keep是解释为$0 ,将打印整行。

I interpreted your requirement as "count the lines in each bracket-delimited section." 我将您的要求解释为“计算每个用括号分隔的部分中的行数”。

$ awk '/^\[.*\]$/ {c=0; print; next} {print ++c, $0}' file
[first]
1 aaa
2 bbb
[second]
1 ccc
2 ddd
3 eee
[third]
1 fff
2 ggg
3 hhh
4 iii
  • /^\\[.*\\]$/ - If the record begins with a literal [ and ends with a literal ] /^\\[.*\\]$/如果记录以文字[开始,以文字]结尾

  • {c=0; print; next} {c=0; print; next} - Set/reset count, print record, skip remaining rules {c=0; print; next} -设置/重置计数,打印记录,跳过剩余规则

  • {print ++c, $0} For records not matching the first rule, print a pre-incremented count, the Output Field Separator , and the record $0 . {print ++c, $0}对于不匹配的第一个规则记录,打印预递增计数时,输出字段分隔符,和记录$0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM