awk Command Reference for CI Scripts
awk processes text record by record, splitting each into fields $1, $2, and so on.
awk shines at column extraction and quick aggregations in pipelines. Most CI bugs come from field-separator assumptions and from quoting the program against the shell.
Common flags/usage
- -F SEP: set the input field separator
- -v name=val: pass a shell value into an awk variable
- $1, $2, $NF: first, second, and last field
- NR / NF: current record number / field count
- BEGIN{} / END{}: run before / after all input
Example
shell
df -h | awk '$5+0 > 80 {print $6, $5}' # mounts over 80% full
awk -F: '{print $1}' /etc/passwd
awk -v limit="${MAX_SIZE}" '$1 > limit' sizes.txtIn CI
Wrap the awk program in single quotes and pass shell values with -v, never by string interpolation, or the shell eats part of the program and you get "syntax error at source line 1". Default whitespace splitting collapses runs of spaces. Debian ships mawk, which lacks some gawk extensions.
Key takeaways
- Single-quote the awk program and inject values with -v to avoid shell mangling.
- -F sets the field separator; default splitting collapses runs of whitespace.
- mawk on Debian lacks some gawk extensions, so avoid version-specific functions.
Related guides
sed Command Reference for CI Scriptssed is a stream editor for find-and-replace in CI builds. Reference for -i, -e, -E, and s///, plus the GNU-vs…
grep Command Reference for CI Scriptsgrep searches text for matching lines in CI. Reference for -r, -E, -i, -q, and -v, plus the exit-code-1-means…
cut Command Reference for CI Scriptscut extracts columns by field or character in CI. Reference for -f, -d, and -c, plus the single-delimiter and…
sort Command Reference for CI Scriptssort orders lines of text in CI for stable diffs. Reference for -n, -u, -k, -t, and -r, plus the locale gotch…