Chapter 4. Reading Input Files
In the typical awk program,
awk reads all input either from the
standard input (by default, this is the keyboard, but often it is a pipe
from another command) or from files whose names you specify on the awk command line. If you specify input files,
awk reads them in order, processing all
the data from one before going on to the next. The name of the current input file can be found in the
predefined variable FILENAME (see Predefined Variables).
The input is read in units called records, and is processed by the rules of your program one record at a time. By default, each record is one line. Each record is automatically split into chunks called fields. This makes it more convenient for programs to work on the parts of a record.
On rare occasions, you may need to use the getline command. The getline command is valuable both because it can do
explicit input from any number of files, and because the files used with it
do not have to be named on the awk
command line (see Explicit Input with getline).
How Input Is Split into Records
awk divides the input for your
program into records and fields. It keeps track of the number of records
that have been read so far from the current input file. This value is stored in a predefined variable called
FNR, which is reset to zero every time
a new file is started. Another predefined variable, NR, records the total number of input records read so far from all datafiles. It starts at zero, but is never automatically reset ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access