gawk: Getline Notes
1
1 4.10.9 Points to Remember About 'getline'
1 -----------------------------------------
1
1 Here are some miscellaneous points about 'getline' that you should bear
1 in mind:
1
1 * When 'getline' changes the value of '$0' and 'NF', 'awk' does _not_
1 automatically jump to the start of the program and start testing
1 the new record against every pattern. However, the new record is
1 tested against any subsequent rules.
1
1 * Some very old 'awk' implementations limit the number of pipelines
1 that an 'awk' program may have open to just one. In 'gawk', there
1 is no such limit. You can open as many pipelines (and coprocesses)
1 as the underlying operating system permits.
1
1 * An interesting side effect occurs if you use 'getline' without a
1 redirection inside a 'BEGIN' rule. Because an unredirected
1 'getline' reads from the command-line data files, the first
1 'getline' command causes 'awk' to set the value of 'FILENAME'.
1 Normally, 'FILENAME' does not have a value inside 'BEGIN' rules,
1 because you have not yet started to process the command-line data
1 files. (d.c.) (See ⇒BEGIN/END; also ⇒Auto-set.)
1
1 * Using 'FILENAME' with 'getline' ('getline < FILENAME') is likely to
1 be a source of confusion. 'awk' opens a separate input stream from
1 the current input file. However, by not using a variable, '$0' and
1 'NF' are still updated. If you're doing this, it's probably by
1 accident, and you should reconsider what it is you're trying to
1 accomplish.
1
1 * ⇒Getline Summary, presents a table summarizing the 'getline'
1 variants and which variables they can affect. It is worth noting
1 that those variants that do not use redirection can cause
1 'FILENAME' to be updated if they cause 'awk' to start reading a new
1 input file.
1
1 * If the variable being assigned is an expression with side effects,
1 different versions of 'awk' behave differently upon encountering
1 end-of-file. Some versions don't evaluate the expression; many
1 versions (including 'gawk') do. Here is an example, courtesy of
1 Duncan Moore:
1
1 BEGIN {
1 system("echo 1 > f")
1 while ((getline a[++c] < "f") > 0) { }
1 print c
1 }
1
1 Here, the side effect is the '++c'. Is 'c' incremented if
1 end-of-file is encountered before the element in 'a' is assigned?
1
1 'gawk' treats 'getline' like a function call, and evaluates the
1 expression 'a[++c]' before attempting to read from 'f'. However,
1 some versions of 'awk' only evaluate the expression once they know
1 that there is a string value to be assigned.
1