为什么"1"在awk中打印当前行? [英] Why does "1" in awk print the current line?
问题描述
在此答案,
awk '$2=="no"{$3="N/A"}1' file
被接受.注意AWK脚本末尾的1
.在评论中,答案的作者说
was accepted. Note the 1
at the end of the AWK script. In the comments, the author of the answer said
[1是]显示当前行的一种神秘方式.
[1 is] a cryptic way to display the current line.
我很困惑.怎么运作的?
I'm puzzled. How does that work?
推荐答案
在awk
,
由于1
始终求值为 true ,因此它将执行默认操作{print $0}
,因此打印存储在$0
Since 1
always evaluates to true, it performs default operation {print $0}
, hence prints the current line stored in $0
因此,awk '$2=="no"{$3="N/A"}1' file
等效于和
awk '$2=="no"{$3="N/A"} {print $0}' file
再次$0
是 print 的默认参数,因此您也可以编写
Again $0
is default argument to print, so you could also write
awk '$2=="no"{$3="N/A"} {print}' file
实际上,您也可以使用任何非零数字或始终评估为 true 的任何条件代替 1
In-fact you could also use any non-zero number or any condition which always evaluates to true in place of 1
这篇关于为什么"1"在awk中打印当前行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!