awk

command module
v1.1.1-0...-c57de61 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 13, 2018 License: MIT Imports: 16 Imported by: 0

README

Notes on the fork

This is a fork of benhoyt/goawk intended as an experiment.

Current changes are:

  • use complex128 as the numeric type instead of float64
  • execute APL expressions echo 1 2 3 | ./awk '{print(apl("+/" $0))}'

Original documentation:

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

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!

Documentation

Overview

Package goawk is an implementation of AWK written in Go.

You can use the command-line "goawk" command or run AWK from your Go programs using the "interp" package. The command-line program has the same interface as regular awk:

goawk [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]

The -F flag specifies the field separator (the default is to split on whitespace). The -v flag allows you to set a variable to a given value (multiple -v flags allowed). The -f flag allows you to read AWK source from a file instead of the 'prog' command-line argument. The rest of the arguments are input filenames (default is to read from stdin).

A simple example (prints the sum of the numbers in the file's second column):

$ echo 'foo 12
> bar 34
> baz 56' >file.txt
$ goawk '{ sum += $2 } END { print sum }' file.txt
102

To use GoAWK in your Go programs, see README.md or the "interp" docs.

Directories

Path Synopsis
internal
ast
Package interp is the GoAWK interpreter (a simple tree-walker).
Package interp is the GoAWK interpreter (a simple tree-walker).
Package lexer is an AWK lexer (tokenizer).
Package lexer is an AWK lexer (tokenizer).
Package parser is an AWK parser and abstract syntax tree.
Package parser is an AWK parser and abstract syntax tree.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL