Go forward to When.
Go backward to Comments.
Go up to Getting Started.
`awk' Statements versus Lines
=============================
Most often, each line in an `awk' program is a separate statement or
separate rule, like this:
awk '/12/ { print $0 }
/21/ { print $0 }' BBS-list inventory-shipped
But sometimes statements can be more than one line, and lines can
contain several statements. You can split a statement into multiple
lines by inserting a newline after any of the following:
, { ? : || && do else
A newline at any other point is considered the end of the statement.
(Splitting lines after `?' and `:' is a minor `gawk' extension. The
`?' and `:' referred to here is the three operand conditional
expression described in See Conditional Expressions: Conditional Exp.)
If you would like to split a single statement into two lines at a
point where a newline would terminate it, you can "continue" it by
ending the first line with a backslash character, `\'. This is allowed
absolutely anywhere in the statement, even in the middle of a string or
regular expression. For example:
awk '/This program is too long, so continue it\
on the next line/ { print $1 }'
We have generally not used backslash continuation in the sample
programs in this manual. Since in `gawk' there is no limit on the
length of a line, it is never strictly necessary; it just makes
programs prettier. We have preferred to make them even more pretty by
keeping the statements short. Backslash continuation is most useful
when your `awk' program is in a separate source file, instead of typed
in on the command line. You should also note that many `awk'
implementations are more picky about where you may use backslash
continuation. For maximal portability of your `awk' programs, it is
best not to split your lines in the middle of a regular expression or a
string.
*Warning: backslash continuation does not work as described above
with the C shell.* Continuation with backslash works for `awk'
programs in files, and also for one-shot programs *provided* you are
using a POSIX-compliant shell, such as the Bourne shell or the
Bourne-again shell. But the C shell used on Berkeley Unix behaves
differently! There, you must use two backslashes in a row, followed by
a newline.
When `awk' statements within one rule are short, you might want to
put more than one of them on a line. You do this by separating the
statements with a semicolon, `;'. This also applies to the rules
themselves. Thus, the previous program could have been written:
/12/ { print $0 } ; /21/ { print $0 }
*Note:* the requirement that rules on the same line must be separated
with a semicolon is a recent change in the `awk' language; it was done
for consistency with the treatment of statements within an action.