简体   繁体   English

AWK getline示例,请解释输出?

[英]AWK getline example , please explain the output?

I found this awk example with getline but not able to understand how it works. 我用getline找到了这个awk示例,但无法理解它是如何工作的。

seq 5 | awk 'BEGIN { getline; print "Read ahead first line", $0 } {print $0 }'

The output is: 输出为:

Read ahead first line 1
2
3
4
5
seq 5 | awk 'BEGIN { getline; print "Read ahead first line", $0 } {print $0 }'

seq 5 you will get seq 5你会得到

$ seq 5
1
2
3
4
5

and from BEGIN { getline; print "Read ahead first line", $0 } 并从BEGIN { getline; print "Read ahead first line", $0 } BEGIN { getline; print "Read ahead first line", $0 } , awk will read first record from stdin thats is 1 BEGIN { getline; print "Read ahead first line", $0 } ,awk将从stdin读取第一条记录,即1

Through pipe seq 5 | awk '{...}' 贯通管seq 5 | awk '{...}' seq 5 | awk '{...}' ( first program | second program ) seq 5 | awk '{...}'first program | second program
Pipes are used to redirect a stream from one program to another. 管道用于将流从一个程序重定向到另一个程序。 When a program's standard output is sent to another through a pipe 当程序的标准输出通过管道发送到另一个时

$ seq 5 | awk 'BEGIN { getline; print "Read ahead first line", $0 }'
Read ahead first line 1

and {print $0 } after BEGIN block will read remaining records that is 2 to 5, here is how without printing inside BEGIN block 和BEGIN块之后的{print $0 }将读取剩余的2到5条记录,这是如何在BEGIN块内不打印的方式

$ seq 5 | awk 'BEGIN { getline} {print $0}'
2
3
4
5

seq 5 produces seq 5产生

1
2
3
4
5

awk 's BEGIN block is executed before the main block. awkBEGIN块在主块之前执行。
getline reads a line, which is printed in the BEGIN block as $0 . getline读取一行,该行在BEGIN块中显示为$0
Then the main block reads the remaining lines. 然后,主块读取剩余的行。

$ seq 5 | awk 'BEGIN { getline; print "BEGIN read", $0} {print "main read", $0 }'
BEGIN read 1
main read 2
main read 3
main read 4
main read 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM