manpagez: man pages & more
info gawk
Home | html | info | man

gawk: Next Statement

 
 7.4.8 The 'next' Statement
 --------------------------
 
 The 'next' statement forces '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 (⇒
 Getline).  That also causes '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, '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 '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 {
          printf("%s:%d: skipped: NF != 4\n", FILENAME, FNR) > "/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.  For more detail, see
 ⇒Special Files.
 
    If the 'next' statement causes the end of the input to be reached,
 then the code in any 'END' rules is executed.  ⇒BEGIN/END.
 
    The 'next' statement is not allowed inside 'BEGINFILE' and 'ENDFILE'
 rules.  ⇒BEGINFILE/ENDFILE.
 
    According to the POSIX standard, the behavior is undefined if the
 'next' statement is used in a 'BEGIN' or 'END' rule.  'gawk' treats it
 as a syntax error.  Although POSIX does not disallow it, most other
 'awk' implementations don't allow the 'next' statement inside function
 bodies (⇒User-defined).  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.
 
© manpagez.com 2000-2018
Individual documents may contain additional copyright information.