BEGIN and END are special patterns. They are not used to match input records. Rather, they are
used for supplying start-up or clean-up information to your awk script. A BEGIN rule is executed,
once, before the fi rst input record has been read. An END rule is executed, once, after all the input
/foo/ { ++foobar }
`foo'. The BEGIN rule prints a title for the report. There is no need to use the BEGIN rule
used for supplying start-up or clean-up information to your awk script. A BEGIN rule is executed,
once, before the fi rst input record has been read. An END rule is executed, once, after all the input
has been read. For example:
BEGIN和END是特殊的模式。它们不用匹配输入的记录。而是应用于在AWK脚本的开始和结束清理信息方面。
BEGIN规则只被执行一次在第一条记录被读取前。和END规则在所有的输入被读取后执行一次。比如。
/foo/ { ++foobar }
END { print "`foo' appears " foobar " times." }' BBS-list
`foo'. The BEGIN rule prints a title for the report. There is no need to use the BEGIN rule
to initialize the counter foobar to zero, as awk does this for us automatically
这个程序找出包含字符“foo“的记录数在“BBS-list"文件中。BEGIN规则在报告前打印标题。
它不需要使用BEGIN规则去初始化计数器foobar到0,AWK会为我们自动去完成这个动作;