gawk: Getline/Variable/File
1
1 4.10.4 Using 'getline' into a Variable from a File
1 --------------------------------------------------
1
1 Use 'getline VAR < FILE' to read input from the file FILE, and put it in
1 the variable VAR. As earlier, FILE is a string-valued expression that
1 specifies the file from which to read.
1
1 In this version of 'getline', none of the predefined variables are
1 changed and the record is not split into fields. The only variable
1 changed is VAR.(1) For example, the following program copies all the
1 input files to the output, except for records that say
1 '@include FILENAME'. Such a record is replaced by the contents of the
1 file FILENAME:
1
1 {
1 if (NF == 2 && $1 == "@include") {
1 while ((getline line < $2) > 0)
1 print line
1 close($2)
1 } else
1 print
1 }
1
1 Note here how the name of the extra input file is not built into the
1 program; it is taken directly from the data, specifically from the
1 second field on the '@include' line.
1
1 The 'close()' function is called to ensure that if two identical
1 '@include' lines appear in the input, the entire specified file is
1 included twice. ⇒Close Files And Pipes.
1
1 One deficiency of this program is that it does not process nested
1 '@include' statements (i.e., '@include' statements in included files)
1 the way a true macro preprocessor would. ⇒Igawk Program for a
1 program that does handle nested '@include' statements.
1
1 ---------- Footnotes ----------
1
1 (1) This is not quite true. 'RT' could be changed if 'RS' is a
1 regular expression.
1