awk Multiple Conditions: && and ||
awk joins tests with && (and), || (or), and ! (not) so one pattern can check several fields together.
Real filters check more than one column: status equals FAIL and duration over a threshold, for instance. && and || compose those tests in a single pattern.
What it does
Logical operators combine boolean expressions in a pattern. && is true when both sides are; || when either is; ! negates. Short-circuit evaluation applies, so the right side of && is skipped if the left is false. Patterns can mix regex, numeric, and field tests.
Common usage
# both conditions must hold
awk '$3 == "FAIL" && $4 > 5 {print $1}' results.txt
# either condition
awk '/ERROR/ || /WARN/ {print NR, $0}' app.log
# negation
awk '!($2 == "OK") {print}' status.txt
# combine a range column with a regex
awk '$5 >= 400 && $7 ~ /api/ {print $7}' access.logOperators
| Operator | What it does |
|---|---|
| a && b | True when both a and b are true |
| a || b | True when either a or b is true |
| !a | Logical negation |
| a ? b : c | Ternary: choose b or c by condition a |
| (a && b) || c | Parentheses to group precedence |
In CI
Compound patterns gate a build on multiple signals in one awk pass, for example failing only when a test is in the slow set and exceeds its budget. Use a ternary in the action (cond ? "WARN" : "OK") to label rows without a separate if.
Common errors in CI
awk uses && and ||, not the words and/or; writing "and" is a syntax error. A single & or | is a different (bitwise or pipe-into) construct, not logical, so doubling matters. Precedence can surprise: && binds tighter than ||, so a || b && c means a || (b && c); add parentheses when in doubt. Comparing a string field with == needs the literal quoted ($3 == "FAIL"), or awk reads FAIL as an unset variable.