awk嵌套卷曲支架
我有下面的awk脚本,我似乎需要下一个花括号。但这在awk中是不允许的。如何在我的脚本中修复此问题 问题在于if(inqueued==1) 尝试改变awk嵌套卷曲支架,awk,parentheses,Awk,Parentheses,我有下面的awk脚本,我似乎需要下一个花括号。但这在awk中是不允许的。如何在我的脚本中修复此问题 问题在于if(inqueued==1) 尝试改变 if(inqueued == 1) { /AttributeConnID/ { connidtext = $0; } /AttributeThisDN / { thisdntext = $2; } #space removes DNRole } 到 或 awk由{}段组
if(inqueued == 1) {
/AttributeConnID/ { connidtext = $0; }
/AttributeThisDN / { thisdntext = $2; } #space removes DNRole
}
到
或
awk由{}
段组成。在
中,可以指定条件,就像在C中使用if
或while
构造一样。您还有一些其他问题,只需将脚本重新编写为:
BEGIN {
print "Log File Analysis Sequencing for", FILENAME
}
/message EventQueued/ {
inqueued=1
print
}
inqueued == 1 {
if (/AttributeConnID/) { connidtext = $0 }
if (/AttributeThisDN/) { thisdntext = $2 } #space removes DNRole
}
#if first chars are a timetamp we know we are out of queued text
/\@?[0-9]+:[0-9}+:[0-9]+/ {
if (thisdntext != 0) {
print connidtext
print thisdntext
}
inqueued=connidtext=thisdntext=""
}
我不知道这是否能满足您的需要,但至少在语法上是正确的。这很有效,谢谢。尽管出于某种原因我得到了一个空白的文件名。我这样调用:awk-f learn5.awk testlog.log>mysequence.txt这是因为您在开始部分进行打印,该部分在打开任何文件之前执行,因此没有文件名。将
BEGIN
更改为FNR==1
。我发现您也可以在BEGIN中使用ARGV[1]。是的,但这不一定是文件名,它可能是一个正在填充的变量,例如,使用awk'{script}'var=“abc”文件
。我建议的方法是你的例子的正确方法。
inqueued == 1 {
if($0~ /AttributeConnID/) { connidtext = $0; }
if($0~/AttributeThisDN /) { thisdntext = $2; } #space removes DNRole
}
inqueued == 1 && /AttributeConnID/{connidtext = $0;}
inqueued == 1 && /AttributeThisDN /{ thisdntext = $2; } #space removes DNRole
BEGIN {
print "Log File Analysis Sequencing for", FILENAME
}
/message EventQueued/ {
inqueued=1
print
}
inqueued == 1 {
if (/AttributeConnID/) { connidtext = $0 }
if (/AttributeThisDN/) { thisdntext = $2 } #space removes DNRole
}
#if first chars are a timetamp we know we are out of queued text
/\@?[0-9]+:[0-9}+:[0-9]+/ {
if (thisdntext != 0) {
print connidtext
print thisdntext
}
inqueued=connidtext=thisdntext=""
}