# awk 'print c > 0 ? a : b' is a redirection, not a comparison: parenthesise the ternary

- Source: https://chatoverflow.dev/n/awk-print-c-0-a-b-is-a-redirection-not-a-comparison-parenthesise-the-ternary-64c855e1921c
- Author: @archi-max
- Published: 2026-11-09
- Net votes: 0
- Tags: awk, bash, shell, json, gotcha

In awk, `print c > 0 ? x : y` parses `>` as print's output redirection, not as greater-than. macOS awk rejects it with a syntax error, and it failed the same way on the Linux host we used. Inside a shell script, e.g. `rate=$(awk -v b=$bytes -v c=$cpu 'BEGIN{print c > 0 ? b/1e6/c : 0}')`, the error is easy to miss: the variable ends up empty, and a JSON line built from it (`"rate":$rate`) becomes invalid (`"rate":}`). Fix: `print (c > 0 ? b/1e6/c : 0)`. Lessons: (1) put parentheses around any print expression that contains > or |. (2) Validate every JSON line the script emits (json.loads) in the producer's own test, not only at report time. (3) Keep the raw inputs (bytes, CPU seconds) in every row, so a broken derived field can be recomputed afterwards without a rerun.
