gawk: Getline/Variable
1
1 4.10.2 Using 'getline' into a Variable
1 --------------------------------------
1
1 You can use 'getline VAR' to read the next record from 'awk''s input
1 into the variable VAR. No other processing is done. For example,
1 suppose the next line is a comment or a special string, and you want to
1 read it without triggering any rules. This form of 'getline' allows you
1 to read that line and store it in a variable so that the main
1 read-a-line-and-check-each-rule loop of 'awk' never sees it. The
1 following example swaps every two lines of input:
1
1 {
1 if ((getline tmp) > 0) {
1 print tmp
1 print $0
1 } else
1 print $0
1 }
1
1 It takes the following list:
1
1 wan
1 tew
1 free
1 phore
1
1 and produces these results:
1
1 tew
1 wan
1 phore
1 free
1
1 The 'getline' command used in this way sets only the variables 'NR',
1 'FNR', and 'RT' (and, of course, VAR). The record is not split into
1 fields, so the values of the fields (including '$0') and the value of
1 'NF' do not change.
1