A fast awk written in Rust. It is a drop-in replacement for the system awk,
built to be used as a full alternative to GNU awk rather than as a teaching
implementation. It starts faster than gawk and is competitive on text
processing, while gawk still leads on numeric-heavy loops; see
Benchmarks.
- Complete POSIX awk: patterns, range patterns,
BEGIN/END,getlinevariants, redirections, user functions, dynamic fields - The GNU extensions that show up in real scripts:
gensub,asort,asorti,strtonum,typeof,isarray,compl,and,or,xor,lshift,rshift,ARGIND,ENVIRON,IGNORECASE,nextfile,mktime,strftime - Correct value model: every scalar carries string and number parts with the
strnumrules POSIX requires, including input fields and-vassignments - Unicode aware: field indexes,
length,substr,matchandRSTARTall count characters instead of bytes - Small and self contained: two dependencies (
regex,chrono), no runtime configuration, single static binary
Prebuilt binaries for Linux, macOS and Windows are attached to every GitHub release.
From crates.io:
$ cargo install awkBuild from source:
$ git clone https://github.com/hakuxyz/awk
$ cd awk
$ cargo build --release
$ install -m 755 target/release/awk /usr/local/bin/awkOr straight into your cargo bin directory:
$ cargo install --path .usage: awk [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]
-F fs field separator (char, string, or regex)
-v var=value assign variable before execution
-f progfile read program from file
--version print version
Exit codes follow gawk: 0 success, 1 for syntax and usage errors (or a
script calling exit 1), 2 for runtime errors such as an unreadable file.
$ echo "hello world" | awk '{print $2}'
world
$ seq 1 100 | awk '{ s += $1 } END { print s }'
5050
$ printf 'apple,3\nbanana,5\n' | awk -F, '{ t += $2 } END { print t }'
8- Patterns, range patterns,
BEGIN/END,next,exit,break,continue - Field splitting with the default whitespace rule, a single character, a
string, or an extended regular expression (
-F '[0-9]+') - Dynamic fields (
$100),NF,NR,FNR,FILENAME,OFS,ORS,SUBSEP,CONVFMT,OFMT,ENVIRON - String functions:
length,substr,index,split,sub,gsub,match,sprintf,toupper,tolower - Math:
int,sqrt,log,exp,sin,cos,atan2,rand,srand printf/sprintfwith%d %i %o %x %X %f %F %e %E %g %G %a %A %s %c %%, field widths, precision, flags, and*width or precision taken from an argument- I/O redirection
>,>>,|for bothprintandgetline getlineforms: plain,var,< file,| cmd, andcmd | getline var
gensubwith\1to\9group references and a match selectorasort,asortiwith optional destination arraystrtonumwith0xhex and leading zero octal prefixestypeofdistinguishinguntyped,unassigned,strnum,number,stringandarrayisarray,compl,and,or,xor,lshift,rshiftIGNORECASEfor matching and field splittingARGIND,RTstyle specials covered where scripts rely on themmktimeandstrftimewith local time, DST handling andLC_TIMEnamesnextfileto skip the rest of the current input file- Hex integer literals such as
0x263a
The crate also exposes the lexer, parser, AST, value model, and interpreter for embedding:
let (funcs, prog) = awk::parser::parse_full(r#"BEGIN { print "hi" }"#).unwrap();
let mut interp = awk::interp::Interp::new(funcs, match prog {
awk::ast::Program::Rules(r) => r,
});
let code = interp.run(&[]);
assert_eq!(code, 0);The library API exists so the CLI engine can be reused, not as a polished public interface. It is not stable yet: while the crate is in 0.x, minor versions may change it, so pin an exact version if you embed it. A documented, stabilized API may follow in a later release.
Two layers keep the behaviour honest:
cargo testruns 39 integration tests against the built CLIpython3 tools/difftest.pyruns 125 programs under both thisawkand the systemgawkand compares stdout, stderr and the exit status
$ cargo test
$ cargo clippy --all-targets
$ python3 tools/difftest.pyCI runs the format check, Clippy, both test layers and the differential
harness on every push (.github/workflows/ci.yml).
tools/bench.sh compares against gawk 5.4.1 on the same machine
(one warm run per workload, milliseconds):
| workload | awk | gawk |
|---|---|---|
| sum 3M records | 3170 | 622 |
print $1 $3, 3M records |
3152 | 705 |
| regex filter 1M records | 4159 | 517 |
| regex FS, 1M records | 1065 | 975 |
| gsub, 1M records | 1976 | 381 |
| string FS, 1M records | 838 | 257 |
startup x1000 (/etc/passwd) |
1466 | 1878 |
The honest summary: startup and small jobs are faster here, and regex field
splitting is on par, but gawk's C interpreter is still several times faster on
record-heavy numeric loops. Run tools/bench.sh to reproduce.
for (k in arr)iterates keys in sorted order. gawk uses hash order, which is unspecified, so sorted order is the deterministic choice here- Error message wording differs, only the exit status matches
compl,andand friends follow gawk's 53 bit masking rules- Some rarely used gawk internals (
FUNCTAB,PROCTAB,bindtextdomain) are not implemented