什么是一些大括号后做什么? [英] What does a number do after curly braces?

查看:154
本文介绍了什么是一些大括号后做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么

echo foo bar..baz bork | awk 'BEGIN{RS=".."} {gsub(OFS,"\t");}1'

似乎做同样的事情。

seem to do the same thing as

echo foo bar..baz bork | awk 'BEGIN{RS=".."} {gsub(OFS,"\t");} {print;}'

在事实上任意数量的不为零(包括小数和底片)会做同样的事情。然而,离开了数字,使用文本字符,或者使用零打印什么。我没有看到这个记录任何地方,虽然我可能错过了一些东西。

In fact any number that isn't zero (including decimals and negatives) will do the same thing. However, leaving off the digit, using a text character, or using zero prints nothing. I didn't see this documented anywhere, although I could have missed something.

推荐答案

如果你还记得, AWK 是有一系列的<$c$c><pattern> &lt;作用&GT; 操作。每个图案为每个线(至少在概念上)进行评价,并且模式匹配时,则执行该操作。任一模式或动作可以省略。被忽略的模式的每一行相匹配;省略的操作默认为 {$打印0} (又名 {打印} )。在'模式'可能是一个简单的regex匹配,或其他一些更复杂,一般情况下,如果动作将被执行(由的在他的<一个埃德莫顿 href=\"http://stackoverflow.com/questions/24643240/what-does-a-number-do-after-curly-braces/24643330?noredirect=1#comment38197609_24643330\">comment).

If you remember, awk is a language which has a series of <pattern> <action> operations. Each pattern is evaluated for each line (at least conceptually), and when the pattern matches, the action is executed. Either the pattern or the action can be omitted. An omitted pattern matches every line; an omitted action defaults to {print $0} (aka {print}). The 'pattern' might be a simple regex match, or some other more complicated and general condition, which must evaluate to true if the action is to be executed (as noted by Ed Morton in his comment).

在您的例子中, 1 是一个模式;它的计算结果为true。动作不指定,所以默认动作被调用,这是 {打印} {打印$ 0} 。大于零或空字符串以外的任何值的计算结果为真,将调用打印。 (请注意,如果你提到一个未初始化的变量(例如, C ),那么它自动创建并设置为零,因此计算结果为false,因此 awk的'C'&LT;&LT;&LT;喜打印无)

In your example, the 1 is a pattern; it evaluates to true. The action is not specified, so the default action is invoked, which is {print} or {print $0}. Any value other than zero or an empty string evaluates to true and will invoke the print. (Note that if you mention an uninitialized variable (for example, c), then it is autocreated and set to zero and therefore evaluates to false. Hence awk 'c' <<<"Hi" prints nothing.)

与关联的动作BEGIN和END模式被特殊处理的,当然。

The actions associated with the BEGIN and END patterns are handled specially, of course.

这篇关于什么是一些大括号后做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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