So far we have been getting our input data from
input stream—either the standard input (usually your keyboard, sometimes
the output from another program) or the
files specified on the command line. The
awk language has a
special built-in command called
can be used to read input under your explicit control.
getline command is used in several different ways and should
not be used by beginners.
The examples that follow the explanation of the
include material that has not been covered yet. Therefore, come back
and study the
getline command after you have reviewed the
this Web page
and have a good knowledge of how
getline command returns 1 if it finds a record and 0 if
it encounters the end of the file. If there is some error in getting
a record, such as a file that cannot be opened, then
returns −1. In this case,
gawk sets the variable
ERRNO to a string describing the error that occurred.
ERRNO indicates that the I/O operation may be
PROCINFO["input", "RETRY"] is set,
getline returns −2
instead of −1, and further calls to
may be attempted. See Retrying Reads After Certain Input Errors for further information about
In the following examples, command stands for a string value that represents a shell command.
NOTE: When --sandbox is specified (see Command-Line Options), reading lines from files, pipes, and coprocesses is disabled.
getlinewith No Arguments
getlineinto a Variable
getlinefrom a File
getlineinto a Variable from a File
getlinefrom a Pipe
getlineinto a Variable from a Pipe
getlinefrom a Coprocess
getlineinto a Variable from a Coprocess