(gawk.info) Boolean Ops
Info Catalog
(gawk.info) Typing and Comparison
(gawk.info) Expressions
(gawk.info) Conditional Exp
Boolean Expressions
===================
A "boolean expression" is a combination of comparison expressions or
matching expressions, using the boolean operators "or" (`||'), "and"
(`&&'), and "not" (`!'), along with parentheses to control nesting.
The truth value of the boolean expression is computed by combining the
truth values of the component expressions. Boolean expressions are
also referred to as "logical expressions". The terms are equivalent.
Boolean expressions can be used wherever comparison and matching
expressions can be used. They can be used in `if', `while', `do' and
`for' statements ( Control Statements in Actions Statements.).
They have numeric values (one if true, zero if false), which come into
play if the result of the boolean expression is stored in a variable, or
used in arithmetic.
In addition, every boolean expression is also a valid pattern, so
you can use one as a pattern to control the execution of rules.
Here are descriptions of the three boolean operators, with examples.
`BOOLEAN1 && BOOLEAN2'
True if both BOOLEAN1 and BOOLEAN2 are true. For example, the
following statement prints the current input record if it contains
both `2400' and `foo'.
if ($0 ~ /2400/ && $0 ~ /foo/) print
The subexpression BOOLEAN2 is evaluated only if BOOLEAN1 is true.
This can make a difference when BOOLEAN2 contains expressions that
have side effects: in the case of `$0 ~ /foo/ && ($2 == bar++)',
the variable `bar' is not incremented if there is no `foo' in the
record.
`BOOLEAN1 || BOOLEAN2'
True if at least one of BOOLEAN1 or BOOLEAN2 is true. For
example, the following statement prints all records in the input
that contain _either_ `2400' or `foo', or both.
if ($0 ~ /2400/ || $0 ~ /foo/) print
The subexpression BOOLEAN2 is evaluated only if BOOLEAN1 is false.
This can make a difference when BOOLEAN2 contains expressions
that have side effects.
`! BOOLEAN'
True if BOOLEAN is false. For example, the following program
prints all records in the input file `BBS-list' that do _not_
contain the string `foo'.
awk '{ if (! ($0 ~ /foo/)) print }' BBS-list
The `&&' and `||' operators are called "short-circuit" operators
because of the way they work. Evaluation of the full expression is
"short-circuited" if the result can be determined part way through its
evaluation.
You can continue a statement that uses `&&' or `||' simply by
putting a newline after them. But you cannot put a newline in front of
either of these operators without using backslash continuation (
`awk' Statements Versus Lines Statements/Lines.).
The actual value of an expression using the `!' operator will be
either one or zero, depending upon the truth value of the expression it
is applied to.
The `!' operator is often useful for changing the sense of a flag
variable from false to true and back again. For example, the following
program is one way to print lines in between special bracketing lines:
$1 == "START" { interested = ! interested }
interested == 1 { print }
$1 == "END" { interested = ! interested }
The variable `interested', like all `awk' variables, starts out
initialized to zero, which is also false. When a line is seen whose
first field is `START', the value of `interested' is toggled to true,
using `!'. The next rule prints lines as long as `interested' is true.
When a line is seen whose first field is `END', `interested' is toggled
back to false.
Info Catalog
(gawk.info) Typing and Comparison
(gawk.info) Expressions
(gawk.info) Conditional Exp
automatically generated byinfo2html