All Projects → benhoyt → Goawk

benhoyt / Goawk

Licence: mit
A POSIX-compliant AWK interpreter written in Go

Programming Languages

go
31211 projects - #10 most used programming language
awk
318 projects

Projects that are alternatives of or similar to Goawk

Internettools
XPath/XQuery 3.1 interpreter for Pascal with compatibility modes for XPath 2.0/XQuery 1.0/3.0, custom and JSONiq extensions, XML/HTML parsers and classes for HTTP/S requests
Stars: ✭ 82 (-91.76%)
Mutual labels:  parser, interpreter
Monkey Rust
An interpreter for the Monkey programming language written in Rust
Stars: ✭ 174 (-82.51%)
Mutual labels:  parser, interpreter
Endbasic
BASIC environment with a REPL, a web interface, and RPi support written in Rust
Stars: ✭ 106 (-89.35%)
Mutual labels:  parser, interpreter
Forge
A lightweight, elegant scripting language with built-in Rust-FFI.
Stars: ✭ 153 (-84.62%)
Mutual labels:  parser, interpreter
Craftinginterpreters
Repository for the book "Crafting Interpreters"
Stars: ✭ 4,298 (+331.96%)
Mutual labels:  parser, interpreter
Rs Monkey Lang
Monkey Programming Language written in Rust.
Stars: ✭ 80 (-91.96%)
Mutual labels:  parser, interpreter
Lioness
The Lioness Programming Language
Stars: ✭ 155 (-84.42%)
Mutual labels:  parser, interpreter
Littlelang
A little language interpreter written in Go
Stars: ✭ 56 (-94.37%)
Mutual labels:  parser, interpreter
Tabloid
A minimal programming language inspired by clickbait headlines
Stars: ✭ 235 (-76.38%)
Mutual labels:  parser, interpreter
Logo
A Logo interpreter written in Swift
Stars: ✭ 207 (-79.2%)
Mutual labels:  parser, interpreter
Cub
The Cub Programming Language
Stars: ✭ 198 (-80.1%)
Mutual labels:  parser, interpreter
Webassemblyjs
Toolchain for WebAssembly
Stars: ✭ 566 (-43.12%)
Mutual labels:  parser, interpreter
Sh
A shell parser, formatter, and interpreter with bash support; includes shfmt
Stars: ✭ 4,343 (+336.48%)
Mutual labels:  parser, interpreter
Mico
Mico ("Monkey" in catalan). Monkey language implementation done with C++. https://interpreterbook.com/
Stars: ✭ 19 (-98.09%)
Mutual labels:  parser, interpreter
Pbparser
Golang library for parsing protocol buffer (.proto) files
Stars: ✭ 30 (-96.98%)
Mutual labels:  parser
Rustpython
A Python Interpreter written in Rust
Stars: ✭ 10,261 (+931.26%)
Mutual labels:  interpreter
Ssp
C++ CSV parser
Stars: ✭ 30 (-96.98%)
Mutual labels:  parser
Mysqllog
Lightweight MySQL slow query log parser in Go
Stars: ✭ 29 (-97.09%)
Mutual labels:  parser
Google Libphonenumber
The up-to-date and reliable Google's libphonenumber package for node.js.
Stars: ✭ 984 (-1.11%)
Mutual labels:  parser
Substitution Schedule Parser
Java library for parsing schools' substitution schedules. Supports multiple different systems mainly used in the German-speaking countries, including Untis, svPlan, and DAVINCI
Stars: ✭ 33 (-96.68%)
Mutual labels:  parser

GoAWK: an AWK interpreter written in Go

Documentation GitHub Actions 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 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!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].