gawk: Boolean Ops
1
1 6.3.3 Boolean Expressions
1 -------------------------
1
1 A "Boolean expression" is a combination of comparison expressions or
1 matching expressions, using the Boolean operators "or" ('||'), "and"
1 ('&&'), and "not" ('!'), along with parentheses to control nesting. The
1 truth value of the Boolean expression is computed by combining the truth
1 values of the component expressions. Boolean expressions are also
1 referred to as "logical expressions". The terms are equivalent.
1
1 Boolean expressions can be used wherever comparison and matching
1 expressions can be used. They can be used in 'if', 'while', 'do', and
1 'for' statements (⇒Statements). They have numeric values (one if
1 true, zero if false) that come into play if the result of the Boolean
1 expression is stored in a variable or used in arithmetic.
1
1 In addition, every Boolean expression is also a valid pattern, so you
1 can use one as a pattern to control the execution of rules. The Boolean
1 operators are:
1
1 'BOOLEAN1 && BOOLEAN2'
1 True if both BOOLEAN1 and BOOLEAN2 are true. For example, the
1 following statement prints the current input record if it contains
1 both 'edu' and 'li':
1
1 if ($0 ~ /edu/ && $0 ~ /li/) print
1
1 The subexpression BOOLEAN2 is evaluated only if BOOLEAN1 is true.
1 This can make a difference when BOOLEAN2 contains expressions that
1 have side effects. In the case of '$0 ~ /foo/ && ($2 == bar++)',
1 the variable 'bar' is not incremented if there is no substring
1 'foo' in the record.
1
1 'BOOLEAN1 || BOOLEAN2'
1 True if at least one of BOOLEAN1 or BOOLEAN2 is true. For example,
1 the following statement prints all records in the input that
1 contain _either_ 'edu' or 'li':
1
1 if ($0 ~ /edu/ || $0 ~ /li/) print
1
1 The subexpression BOOLEAN2 is evaluated only if BOOLEAN1 is false.
1 This can make a difference when BOOLEAN2 contains expressions that
1 have side effects. (Thus, this test never really distinguishes
1 records that contain both 'edu' and 'li'--as soon as 'edu' is
1 matched, the full test succeeds.)
1
1 '! BOOLEAN'
1 True if BOOLEAN is false. For example, the following program
1 prints 'no home!' in the unusual event that the 'HOME' environment
1 variable is not defined:
1
1 BEGIN { if (! ("HOME" in ENVIRON))
1 print "no home!" }
1
1 (The 'in' operator is described in ⇒Reference to Elements.)
1
1 The '&&' and '||' operators are called "short-circuit" operators
1 because of the way they work. Evaluation of the full expression is
1 "short-circuited" if the result can be determined partway through its
1 evaluation.
1
1 Statements that end with '&&' or '||' can be continued simply by
1 putting a newline after them. But you cannot put a newline in front of
11 either of these operators without using backslash continuation (⇒
Statements/Lines).
1
1 The actual value of an expression using the '!' operator is either
1 one or zero, depending upon the truth value of the expression it is
1 applied to. The '!' operator is often useful for changing the sense of
1 a flag variable from false to true and back again. For example, the
1 following program is one way to print lines in between special
1 bracketing lines:
1
1 $1 == "START" { interested = ! interested; next }
1 interested { print }
1 $1 == "END" { interested = ! interested; next }
1
1 The variable 'interested', as with all 'awk' variables, starts out
1 initialized to zero, which is also false. When a line is seen whose
1 first field is 'START', the value of 'interested' is toggled to true,
1 using '!'. The next rule prints lines as long as 'interested' is true.
1 When a line is seen whose first field is 'END', 'interested' is toggled
1 back to false.(1)
1
1 Most commonly, the '!' operator is used in the conditions of 'if' and
1 'while' statements, where it often makes more sense to phrase the logic
1 in the negative:
1
1 if (! SOME CONDITION || SOME OTHER CONDITION) {
1 ... DO WHATEVER PROCESSING ...
1 }
1
1 NOTE: The 'next' statement is discussed in ⇒Next Statement.
1 'next' tells 'awk' to skip the rest of the rules, get the next
1 record, and start processing the rules over again at the top. The
1 reason it's there is to avoid printing the bracketing 'START' and
1 'END' lines.
1
1 ---------- Footnotes ----------
1
1 (1) This program has a bug; it prints lines starting with 'END'. How
1 would you fix it?
1