Go to the first, previous, next, last section, table of contents.
next Statement
The next statement forces @command{awk} to immediately stop processing
the current record and go on to the next record. This means that no
further rules are executed for the current record, and the rest of the
current rule's action isn't executed.
Contrast this with the effect of the getline function
(see section Explicit Input with getline). That also causes
@command{awk} to read the next record immediately, but it does not alter the
flow of control in any way (i.e., the rest of the current action executes
with a new input record).
At the highest level, @command{awk} program execution is a loop that reads
an input record and then tests each rule's pattern against it. If you
think of this loop as a for statement whose body contains the
rules, then the next statement is analogous to a continue
statement. It skips to the end of the body of this implicit loop and
executes the increment (which reads another record).
For example, suppose an @command{awk} program works only on records with four fields, and it shouldn't fail when given bad input. To avoid complicating the rest of the program, write a "weed out" rule near the beginning, in the following manner:
NF != 4 {
err = sprintf("%s:%d: skipped: NF != 4\n", FILENAME, FNR)
print err > "/dev/stderr"
next
}
Because of the next statement,
the program's subsequent rules won't see the bad record. The error
message is redirected to the standard error output stream, as error
messages should be.
@xref{Special Files, ,Special File Names in @command{gawk}}.
According to the POSIX standard, the behavior is undefined if
the next statement is used in a BEGIN or END rule.
@command{gawk} treats it as a syntax error.
Although POSIX permits it,
some other @command{awk} implementations don't allow the next
statement inside function bodies
(see section User-Defined Functions).
Just as with any other next statement, a next statement inside a
function body reads the next record and starts processing it with the
first rule in the program.
If the next statement causes the end of the input to be reached,
then the code in any END rules is executed.
See section The BEGIN and END Special Patterns.
Go to the first, previous, next, last section, table of contents.