Gawk/Getline-Notes
Next: Getline Summary, Previous: Getline/Variable/Coprocess, Up: Getline [Contents][Index]
4.10.9 Points to Remember About getline
Here are some miscellaneous points about getline
that
you should bear in mind:
- When
getline
changes the value of$0
andNF
,awk
does not automatically jump to the start of the program and start testing the new record against every pattern. However, the new record is tested against any subsequent rules. - Some very old
awk
implementations limit the number of pipelines that anawk
program may have open to just one. Ingawk
, there is no such limit. You can open as many pipelines (and coprocesses) as the underlying operating system permits. - An interesting side effect occurs if you use
getline
without a redirection inside aBEGIN
rule. Because an unredirectedgetline
reads from the command-line data files, the firstgetline
command causesawk
to set the value ofFILENAME
. Normally,FILENAME
does not have a value insideBEGIN
rules, because you have not yet started to process the command-line data files. (d.c.) (See TheBEGIN
andEND
Special Patterns; also see section Built-in Variables That Convey Information.) - Using
FILENAME
withgetline
(‘getline < FILENAME
’) is likely to be a source of confusion.awk
opens a separate input stream from the current input file. However, by not using a variable,$0
andNF
are still updated. If you’re doing this, it’s probably by accident, and you should reconsider what it is you’re trying to accomplish. - Summary of
getline
Variants, presents a table summarizing thegetline
variants and which variables they can affect. It is worth noting that those variants that do not use redirection can causeFILENAME
to be updated if they causeawk
to start reading a new input file. If the variable being assigned is an expression with side effects, different versions of
awk
behave differently upon encountering end-of-file. Some versions don’t evaluate the expression; many versions (includinggawk
) do. Here is an example, courtesy of Duncan Moore:BEGIN { system("echo 1 > f") while ((getline a[++c] < "f") > 0) { } print c }
Here, the side effect is the ‘
++c
’. Isc
incremented if end-of-file is encountered before the element ina
is assigned?gawk
treatsgetline
like a function call, and evaluates the expression ‘a[++c]
’ before attempting to read fromf
. However, some versions ofawk
only evaluate the expression once they know that there is a string value to be assigned.
Next: Getline Summary, Previous: Getline/Variable/Coprocess, Up: Getline [Contents][Index]