Skip to content
GoAWK: an AWK interpreter written in Go
Go Python Shell
Branch: master
Clone or download
benhoyt Fix split() to have same behavior for single-char FS
Fix case of empty input line (NF should be 0)
Latest commit 1a4e25c May 27, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
fuzz
internal Use faster version of TrimSpace to speed up numeric string handling Dec 7, 2018
interp
lexer Support hex and octal string escape sequences (\xhh and \ooo) Jan 10, 2019
parser Add support for "delete a" (not POSIX but awk/gawk/mawk support it, a… Jan 13, 2019
testdata Add support for "delete a" (not POSIX but awk/gawk/mawk support it, a… Jan 13, 2019
.gitignore Add script to build cross-platform binaries Apr 27, 2019
.travis.yml Trying to get "go test ." working under TravisCI again Nov 7, 2018
LICENSE.txt
README.md Add note about Windows quoting to README May 8, 2019
TODO.txt Add config options for "safe mode" to prevent exec and file reads/writes Jan 19, 2019
appveyor.yml No need to upload artifacts to AppVeyor (we build them all with cross… May 8, 2019
benchmark.sh Add benchmark and benchstat scripts Sep 6, 2018
benchmark_awks.py Make benchmark script use speeds compared to awk instead of times Dec 8, 2018
benchmarks.txt Update benchmarks, add more tests for numstr truthiness (some gaps ri… Jan 12, 2019
benchstat.sh Add benchmark and benchstat scripts Sep 6, 2018
go.mod Add go.mod to allow goawk to be used as module Nov 17, 2018
goawk.go Fix handling of single-character FS May 27, 2019
goawk_test.go Parse cmdline manually (no "flag" package) to allow -F: and the like Apr 26, 2019
make_binaries.sh Add script to build cross-platform binaries Apr 27, 2019

README.md

GoAWK: an AWK interpreter written in Go

GoDoc TravisCI Build AppVeyor Build

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" test suite.

Read more about how GoAWK works and performs here.

Basic usage

To use the command-line version, simply use go get to install it, and then run it using goawk (assuming $GOPATH/bin is in your PATH):

$ go get github.com/benhoyt/goawk
$ goawk 'BEGIN { print "foo", 42 }'
foo 42
$ echo 1 2 3 | goawk '{ print $1 + $3 }'
4

On Windows, " is the shell quoting character, so use " around the entire AWK program on the command line, and use ' around AWK strings -- this is a non-POSIX extension to make GoAWK easier to use on Windows:

C:\> goawk "BEGIN { print 'foo', 42 }"
foo 42

To use it in your Go programs, you can call interp.Exec() directly for simple needs:

input := bytes.NewReader([]byte("foo bar\n\nbaz buz"))
err := interp.Exec("$0 { print $1 }", " ", input, nil)
if err != nil {
    fmt.Println(err)
    return
}
// Output:
// foo
// baz

Or you can use the parser module and then interp.ExecProgram() to control execution, set variables, etc:

src := "{ print NR, tolower($0) }"
input := "A\naB\nAbC"

prog, err := parser.ParseProgram([]byte(src), nil)
if err != nil {
    fmt.Println(err)
    return
}
config := &interp.Config{
    Stdin: bytes.NewReader([]byte(input)),
    Vars:  []string{"OFS", ":"},
}
_, err = interp.ExecProgram(prog, config)
if err != nil {
    fmt.Println(err)
    return
}
// Output:
// 1:a
// 2:ab
// 3:abc

Read the GoDoc documentation for more details.

Differences from AWK

The 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:

  • It's embeddable in your Go programs! You can even call custom Go functions from your AWK scripts.
  • I/O-bound AWK scripts (which is most of them) are significantly faster than awk, and on a par with gawk and mawk.
  • The parser supports 'single-quoted strings' in addition to "double-quoted strings", primarily to make Windows one-liners easier (the Windows cmd.exe shell uses " as the quote character).

Things AWK has over GoAWK:

  • CPU-bound AWK scripts are slightly slower than awk, and about twice as slow as gawk and mawk.
  • AWK is written by Brian Kernighan.

Stability

This project has a good suite of tests, and I've used it a bunch personally, but it's certainly not battle-tested or heavily used, so please use at your own risk. I intend not to change the Go API in a breaking way.

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!

You can’t perform that action at this time.