(gawk) Plain Getline
Info Catalog
(gawk) Getline Intro
(gawk) Getline
(gawk) Getline/Variable
Using `getline' with No Arguments
---------------------------------
The `getline' command can be used without arguments to read input
from the current input file. All it does in this case is read the next
input record and split it up into fields. This is useful if you've
finished processing the current record, but you want to do some special
processing _right now_ on the next record. Here's an example:
awk '{
if ((t = index($0, "/*")) != 0) {
# value will be "" if t is 1
tmp = substr($0, 1, t - 1)
u = index(substr($0, t + 2), "*/")
while (u == 0) {
if (getline <= 0) {
m = "unexpected EOF or error"
m = (m ": " ERRNO)
print m > "/dev/stderr"
exit
}
t = -1
u = index($0, "*/")
}
# substr expression will be "" if */
# occurred at end of line
$0 = tmp substr($0, t + u + 3)
}
print $0
}'
This `awk' program deletes all C-style comments, `/* ... */', from
the input. By replacing the `print $0' with other statements, you
could perform more complicated processing on the decommented input,
like searching for matches of a regular expression. This program has a
subtle problem--it does not work if one comment ends and another begins
on the same line.
This form of the `getline' command sets `NF' (the number of fields;
Examining Fields Fields.), `NR' (the number of records read so
far; How Input is Split into Records Records.), `FNR' (the
number of records read from this input file), and the value of `$0'.
The original value of `$0' that triggered the rule
which executed `getline' is lost (d.c.). By contrast, the `next'
statement reads a new record but immediately begins processing it
normally, starting with the first rule in the program. The
`next' Statement Next Statement.
Info Catalog
(gawk) Getline Intro
(gawk) Getline
(gawk) Getline/Variable
automatically generated byinfo2html