gawk: Getline/File
1
1 4.10.3 Using 'getline' from a File
1 ----------------------------------
1
1 Use 'getline < FILE' to read the next record from FILE. Here, FILE is a
1 string-valued expression that specifies the file name. '< FILE' is
1 called a "redirection" because it directs input to come from a different
1 place. For example, the following program reads its input record from
1 the file 'secondary.input' when it encounters a first field with a value
1 equal to 10 in the current input file:
1
1 {
1 if ($1 == 10) {
1 getline < "secondary.input"
1 print
1 } else
1 print
1 }
1
1 Because the main input stream is not used, the values of 'NR' and
1 'FNR' are not changed. However, the record it reads is split into
1 fields in the normal manner, so the values of '$0' and the other fields
1 are changed, resulting in a new value of 'NF'. 'RT' is also set.
1
1 According to POSIX, 'getline < EXPRESSION' is ambiguous if EXPRESSION
1 contains unparenthesized operators other than '$'; for example, 'getline
1 < dir "/" file' is ambiguous because the concatenation operator (not
1 discussed yet; ⇒Concatenation) is not parenthesized. You should
1 write it as 'getline < (dir "/" file)' if you want your program to be
1 portable to all 'awk' implementations.
1