【发布时间】:2018-10-24 16:14:49
【问题描述】:
我有以下 awk 程序:
/.*needle.*/
{
if ($0 != "hay needle hay")
{
print "yay: ", $1;
next;
}
print "ya2";
next;
}
{
print "no";
next;
}
我在GNU Awk 4.2.1, API: 2.0 中以gawk -f test.awk < some.log > out.log 运行它。
一些日志:
hay hay hay
hay needle hay
needle
hay hay
hay
hay
out.log:
yay: hay
hay needle hay
ya2
needle
yay: needle
yay: hay
yay: hay
yay: hay
我希望它只打印“ya2-new line-yay: needle”。
这引发了一些问题:
- 为什么即使在不匹配的行上也会调用模式操作,而不是无条件操作?
The purpose of the action is to tell awk what to do once a match for the pattern is found。 - 为什么
next;不抑制匹配行的打印,如printis only the "default" action?我们在这里有一个非默认操作。The next statement forces awk to immediately stop processing the current record and go on to the next record - 我可以并且确实在默认操作中将
match()调用放入if()中,但为什么这个变体不起作用?
【问题讨论】:
-
我在不同的数据上尝试了一个非常相似的表格:
awk '/424/ {if (NR==3) {print;next;}print NR;next;} {print "no";}'输出符合预期(如果“424”在该行中,则打印行号,但在第 3 行打印该行时除外。“否" 以其他方式打印。)也傻眼。 -
我认为
fields[1]是一些..construct 我不知道这是有道理的。$1,您在上面使用的当然是打印第一列的正常方式。 -
在最小化 MCV 时破坏它。让我解决它。