gawk: Statements/Lines

1 
1 1.6 'awk' Statements Versus Lines
1 =================================
1 
1 Most often, each line in an 'awk' program is a separate statement or
1 separate rule, like this:
1 
1      awk '/12/  { print $0 }
1           /21/  { print $0 }' mail-list inventory-shipped
1 
1    However, 'gawk' ignores newlines after any of the following symbols
1 and keywords:
1 
1      ,    {    ?    :    ||    &&    do    else
1 
1 A newline at any other point is considered the end of the statement.(1)
1 
1    If you would like to split a single statement into two lines at a
1 point where a newline would terminate it, you can "continue" it by
1 ending the first line with a backslash character ('\').  The backslash
1 must be the final character on the line in order to be recognized as a
1 continuation character.  A backslash is allowed anywhere in the
1 statement, even in the middle of a string or regular expression.  For
1 example:
1 
1      awk '/This regular expression is too long, so continue it\
1       on the next line/ { print $1 }'
1 
1 We have generally not used backslash continuation in our sample
1 programs.  'gawk' places no limit on the length of a line, so backslash
1 continuation is never strictly necessary; it just makes programs more
1 readable.  For this same reason, as well as for clarity, we have kept
1 most statements short in the programs presented throughout the Info
1 file.  Backslash continuation is most useful when your 'awk' program is
1 in a separate source file instead of entered from the command line.  You
1 should also note that many 'awk' implementations are more particular
1 about where you may use backslash continuation.  For example, they may
1 not allow you to split a string constant using backslash continuation.
1 Thus, for maximum portability of your 'awk' programs, it is best not to
1 split your lines in the middle of a regular expression or a string.
1 
1      CAUTION: _Backslash continuation does not work as described with
1      the C shell._  It works for 'awk' programs in files and for
1      one-shot programs, _provided_ you are using a POSIX-compliant
1      shell, such as the Unix Bourne shell or Bash.  But the C shell
1      behaves differently!  There you must use two backslashes in a row,
1      followed by a newline.  Note also that when using the C shell,
1      _every_ newline in your 'awk' program must be escaped with a
1      backslash.  To illustrate:
1 
1           % awk 'BEGIN { \
1           ?   print \\
1           ?       "hello, world" \
1           ? }'
1           -| hello, world
1 
1      Here, the '%' and '?' are the C shell's primary and secondary
1      prompts, analogous to the standard shell's '$' and '>'.
1 
1      Compare the previous example to how it is done with a
1      POSIX-compliant shell:
1 
1           $ awk 'BEGIN {
1           >   print \
1           >       "hello, world"
1           > }'
1           -| hello, world
1 
1    'awk' is a line-oriented language.  Each rule's action has to begin
1 on the same line as the pattern.  To have the pattern and action on
1 separate lines, you _must_ use backslash continuation; there is no other
1 option.
1 
1    Another thing to keep in mind is that backslash continuation and
1 comments do not mix.  As soon as 'awk' sees the '#' that starts a
1 comment, it ignores _everything_ on the rest of the line.  For example:
1 
1      $ gawk 'BEGIN { print "dont panic" # a friendly \
1      >                                    BEGIN rule
1      > }'
1      error-> gawk: cmd. line:2:                BEGIN rule
1      error-> gawk: cmd. line:2:                ^ syntax error
1 
1 In this case, it looks like the backslash would continue the comment
1 onto the next line.  However, the backslash-newline combination is never
1 even noticed because it is "hidden" inside the comment.  Thus, the
1 'BEGIN' is noted as a syntax error.
1 
1    When 'awk' statements within one rule are short, you might want to
1 put more than one of them on a line.  This is accomplished by separating
1 the statements with a semicolon (';').  This also applies to the rules
1 themselves.  Thus, the program shown at the start of this minor node
1 could also be written this way:
1 
1      /12/ { print $0 } ; /21/ { print $0 }
1 
1      NOTE: The requirement that states that rules on the same line must
1      be separated with a semicolon was not in the original 'awk'
1      language; it was added for consistency with the treatment of
1      statements within an action.
1 
1    ---------- Footnotes ----------
1 
1    (1) The '?' and ':' referred to here is the three-operand conditional
1 expression described in ⇒Conditional Exp.  Splitting lines after
1 '?' and ':' is a minor 'gawk' extension; if '--posix' is specified
1 (⇒Options), then this extension is disabled.
1