awk:为什么匹配函数打印匹配的行而不打印它
我有以下内容:awk:为什么匹配函数打印匹配的行而不打印它,awk,Awk,我有以下内容: NODE_1 port 1 description blah port 2 description blah blah NODE_2 port 1 description blah port 2 description blah NODE_3 port 1 port 2 NODE_4 port 1 port 2 NODE_5 port 1 port 2 NODE_6 port 1 description blahdy blah port 2 description floop-a
NODE_1
port 1
description blah
port 2
description blah blah
NODE_2
port 1
description blah
port 2
description blah
NODE_3
port 1
port 2
NODE_4
port 1
port 2
NODE_5
port 1
port 2
NODE_6
port 1
description blahdy blah
port 2
description floop-a-doop
我正在尝试打印节点的前三个匹配项的匹配属性
awk 'BEGIN{count=0}
match($0,/NODE/,a)
{
if(RSTART != 0){
print "*******************************"
for (i in a)
print i"-->"a[i]
count++;
print "count-->"count;
print "*******************************"
}
if (count >= 3)
{
exit
}
}' awksampledata5.txt
输出是
NODE_1
*******************************
0start-->1
0length-->4
0-->NODE
count-->1
*******************************
NODE_2
*******************************
0start-->1
0length-->4
0-->NODE
count-->2
*******************************
NODE_3
*******************************
0start-->1
0length-->4
0-->NODE
count-->3
*******************************
我不想打印节点1、节点2和节点3。但我不知道它是怎么印出来的
答案编辑代码:
$ awk 'BEGIN{count=0}
match($0,/NODE/,a){
if(RSTART != 0){ <-- this matters new lines matters.
print "*******************************"
for (i in a)
print i"-->"a[i]
count++;
print "count-->"count;
print "*******************************"
}
if (count >= 3)
{
exit
}
}' awksampledata5.txt
*******************************
0start-->1
0length-->4
0-->NODE
count-->1
*******************************
*******************************
0start-->1
0length-->4
0-->NODE
count-->2
*******************************
*******************************
0start-->1
0length-->4
0-->NODE
count-->3
*******************************
$awk'开始{count=0}
匹配($0,/NODE/,a){
如果(RSTART!=0){=3)
{
出口
}
}'awksampledata5.txt
*******************************
0开始-->1
0长度-->4
0-->节点
计数-->1
*******************************
*******************************
0开始-->1
0长度-->4
0-->节点
计数-->2
*******************************
*******************************
0开始-->1
0长度-->4
0-->节点
计数-->3
*******************************
换行符很重要。这:
match($0,/foo/)
{ bar() }
与以下任何一项都不相同:
match($0,/foo/) { bar() }
match($0,/foo/) {
bar()
}
第一个剧本说
If "foo" exists in $0 then print the current record.
Call bar().
而另外两个说:
If "foo" exists in $0 then call bar().
wrt
for(a中的i)
-在a
中只有一件事,那就是a[0]
,它将包含文本节点
,因为这是您在/NODE/
中告诉它要匹配的内容。不确定您在那里试图做什么,但请尝试以下操作:匹配($0,/(N)(OD)(E)/,a)
,然后循环查看如何匹配()works wrt填充数组。另外-您不需要单独测试RSTART,因为它在该块中始终为非零。这是真的,我也想尝试分组。但这个问题我想保留它更多用于打印匹配线,为什么会发生解释,这就是为什么上面是一个注释,而不是我答案的一部分。