Skip to content
Latchkey

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.txt

In 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →