AWK is a fascinating text-processing language, and somehow after reading the delightfully-terse The AWK Programming Language I was inspired to write an interpreter for it in Go. So here it is, feature-complete and tested against “the one true AWK” and GNU AWK test suites.
GoAWK is a POSIX-compatible version of AWK, and additionally has a CSV mode for reading and writing CSV and TSV files (read the CSV documentation).
You can also read one of the articles I’ve written about GoAWK:
To use the command-line version, download one of the release binaries. You can also use go install to build and install it from source, and then run it using goawk (assuming ~/go/bin is in your PATH):
$ go install github.com/benhoyt/goawk@latest
$ goawk 'BEGIN { print "foo", 42 }'
foo 42
$ echo 1 2 3 | goawk '{ print $1 + $3 }'
4
# Or use GoAWK's CSV and @"named-field" support:
$ echo -e 'name,amount\nBob,17.50\nJill,20\n"Boba Fett",100.00' | \
goawk -i csv -H '{ total += @"amount" } END { print total }'
137.5
To use it in your Go programs, you can call interp.Exec directly for simple needs:
If you need to repeat execution of the same program on different inputs, you can call interp.New once, and then call the returned object’s Execute method as many times as you need.
It supports negative field indexes to access fields from the right, for example, $-1 refers to the last field.
It’s embeddable in your Go programs! You can even call custom Go functions from your AWK scripts.
Most AWK scripts are faster than awk and on a par with gawk, though usually slower than mawk.
The parser supports 'single-quoted strings' in addition to "double-quoted strings", primarily to make Windows one-liners easier when using the cmd.exe shell (which uses " as the quote character).
Things AWK has over GoAWK:
Scripts that use regular expressions are slower than other implementations (unfortunately Go’s regexp package is relatively slow).
AWK is written by Alfred Aho, Peter Weinberger, and Brian Kernighan.
Stability
This project has a good suite of tests, which include my own intepreter tests, the original AWK test suite, and the relevant tests from the Gawk test suite. I’ve used it a bunch personally, and it’s used in the Benthos stream processor as well as by the software team at the library of the University of Antwerp. However, to err == human, so please use GoAWK at your own risk. I intend not to change the Go API in a breaking way in any v1.x.y version.
AWKGo
The GoAWK repository also includes AWKGo, an AWK-to-Go compiler. This is experimental and is not subject to the stability requirements of GoAWK itself. You can read more about AWKGo or browse the code on the awkgo branch.
License
GoAWK is licensed under an open source MIT license.
The end
Have fun, and please contact me if you’re using GoAWK or have any feedback!
GoAWK: an AWK interpreter with CSV support
AWK is a fascinating text-processing language, and somehow after reading the delightfully-terse The AWK Programming Language I was inspired to write an interpreter for it in Go. So here it is, feature-complete and tested against “the one true AWK” and GNU AWK test suites.
GoAWK is a POSIX-compatible version of AWK, and additionally has a CSV mode for reading and writing CSV and TSV files (read the CSV documentation).
You can also read one of the articles I’ve written about GoAWK:
Basic usage
To use the command-line version, download one of the release binaries. You can also use
go installto build and install it from source, and then run it usinggoawk(assuming~/go/binis in yourPATH):To use it in your Go programs, you can call
interp.Execdirectly for simple needs:Or you can use
parser.ParseProgramand theninterp.ExecProgramto control execution, set variables, and so on:If you need to repeat execution of the same program on different inputs, you can call
interp.Newonce, and then call the returned object’sExecutemethod as many times as you need.Read the Go package documentation for more details.
Differences from AWK
My intention is for GoAWK to conform to
awk‘s behavior and to the POSIX AWK spec, but this section describes some areas where it’s different.Additional features GoAWK has over AWK:
awkandgawkrecently added basic CSV support too, with the--csvoption.$-1refers to the last field.awkand on a par withgawk, though usually slower thanmawk.'single-quoted strings'in addition to"double-quoted strings", primarily to make Windows one-liners easier when using thecmd.exeshell (which uses"as the quote character).Things AWK has over GoAWK:
regexppackage is relatively slow).Stability
This project has a good suite of tests, which include my own intepreter tests, the original AWK test suite, and the relevant tests from the Gawk test suite. I’ve used it a bunch personally, and it’s used in the Benthos stream processor as well as by the software team at the library of the University of Antwerp. However, to
err == human, so please use GoAWK at your own risk. I intend not to change the Go API in a breaking way in any v1.x.y version.AWKGo
The GoAWK repository also includes AWKGo, an AWK-to-Go compiler. This is experimental and is not subject to the stability requirements of GoAWK itself. You can read more about AWKGo or browse the code on the
awkgobranch.License
GoAWK is licensed under an open source MIT license.
The end
Have fun, and please contact me if you’re using GoAWK or have any feedback!