36

GoAWK: an AWK interpreter written in Go

 5 years ago
source link: https://www.tuicool.com/articles/hit/RBnmeyj
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

GoAWK: an AWK interpreter written in Go

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, pretty much feature-complete and tested against "the one true AWK" test suite.

To use the command-line version, simply use go get to install it, and then run it using goawk :

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

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.New() and Interp.Exec() to control execution, set variables, etc:

src := "{ print $1+$2 }"
input := "1,2\n3,4\n5,6"

prog, err := parser.ParseProgram([]byte(src))
if err != nil {
    fmt.Println(err)
    return
}
p := interp.New(nil, nil)
p.SetVar("FS", ",")
err = p.Exec(prog, bytes.NewReader([]byte(input)), nil)
if err != nil {
    fmt.Println(err)
    return
}
// Output:
// 3
// 7
// 11

Have fun, and please contact me if you're using GoAWK or have any feedback!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK