Prig is for Processing Records In Go. Like AWK, but snobbish.

Prig: the snobbish AWK

Prig is for Processing Records In Go. It's like AWK, but snobbish (Go! static typing!). It's also faster to execute, and if you know Go, you don't need to learn AWK.

You can also read my article on why and how I wrote Prig. The tl;dr is "no good reason, I'm a geek!" :-)

How to use Prig

To install prig, make sure Go is installed and then type go install github.com/benhoyt/prig@latest. Prig itself runs the generated code using go build, so even once you have a prig executable it requires the Go compiler to be installed.

As a simple example, you can try the following script. It prints a modified version of the second field of each line of input (the full URL in this example):

$ cat logs.txt
GET /robots.txt HTTP/1.1
HEAD /README.md HTTP/1.1
GET /wp-admin/ HTTP/1.0

$ prig 'Println("https://example.com" + S(2))' <logs.txt
https://example.com/robots.txt
https://example.com/README.md
https://example.com/wp-admin/

To get help, run prig with a -h or --help argument. Help output is copied below, showing more examples at the bottom:

Prig v1.0.0 - Copyright (c) 2022 Ben Hoyt

Usage: prig [options] [-b 'begin code'] 'per-record code' [-e 'end code']

Prig is for Processing Records In Go. It's like AWK, but snobbish (Go! static
typing!). It runs 'begin code' first, then runs 'per-record code' for every
record (line) in the input, then runs 'end code'. Prig uses "go build", so it
requires the Go compiler: https://go.dev/doc/install

Options:
  -F char | re     field separator (single character or multi-char regex)
  -g executable    Go compiler to use (eg: "go1.18rc1", default "go")
  -h, --help       print help message and exit
  -i import        import Go package (normally automatic)
  -s               print formatted Go source instead of running
  -V, --version    print version number and exit

Built-in functions:
  F(i int) float64 // return field i as float64, int, or string
  I(i int) int     // (i==0 is entire record, i==1 is first field)
  S(i int) string

  NF() int // return number of fields in current record
  NR() int // return number of current record

  Print(args ...interface{})                 // fmt.Print, but buffered
  Printf(format string, args ...interface{}) // fmt.Printf, but buffered
  Println(args ...interface{})               // fmt.Println, but buffered

  Match(re, s string) bool            // report whether s contains match of re
  Replace(re, s, repl string) string  // replace all re matches in s with repl
  Submatches(re, s string) []string   // return slice of submatches of re in s
  Substr(s string, n[, m] int) string // s[n:m] but safe and allow negative n/m

  Sort[T int|float64|string](s []T) []T
    // return new sorted slice; also Sort(s, Reverse) to sort descending
  SortMap[T int|float64|string](m map[string]T) []KV[T]
    // return sorted slice of key-value pairs
    // also Sort(s[, Reverse][, ByValue]) to sort descending or by value

Examples:
  # Run an arbitrary Go snippet; don't process input
  prig -b 'Println("Hello, world!", math.Pi)'

  # Print the average value of the last field
  prig -b 's := 0.0' 's += F(NF())' -e 'Println(s / float64(NR()))'

  # Print 3rd field in milliseconds if line contains "GET" or "HEAD"
  prig 'if Match(`GET|HEAD`, S(0)) { Printf("%.0fms\n", F(3)*1000) }'

  # Print frequencies of unique words, most frequent first
  prig -b 'freqs := map[string]int{}' \
       'for i := 1; i <= NF(); i++ { freqs[strings.ToLower(S(i))]++ }' \
       -e 'for _, f := range SortMap(freqs, ByValue, Reverse) { ' \
       -e 'Println(f.K, f.V) }'

Prig uses the golang.org/x/tools/imports package, so imports are usually automatic (use -i if you need to disambiguate, for example between text/template and html/template). And that's really all you need to know -- the code snippets are pure Go.

Other information

If you're looking for a real, POSIX-compatible version of AWK for use in Go programs, see my GoAWK project.

Prig was based on rp, a similar idea for Nim written by Charles Blake.

Prig is licensed under an open source MIT license.

Owner
Ben Hoyt
Software engineer at Canonical. Author of GoAWK, inih, and Python's os.scandir(). Technical writer.
Ben Hoyt
Similar Resources

A simple Go utility to display track information from, and send commands to, spotifyd from Tiling Window Managers like Sway and i3

A simple Go utility to display track information from, and send commands to, spotifyd from Tiling Window Managers like Sway and i3

Untitled Spotifyd Controller A simple Go utility to display track information from, and send commands to, spotifyd from Tiling Window Managers like Sw

Mar 8, 2022

🚀 A command with fzf-like UI to quickly search Wikipedia articles and open it in your browser

fzwiki A command with fzf-like UI to quickly search Wikipedia articles and open it in your browser. Usage Run the command by specifying a search query

Dec 20, 2022

DND-magic-item-Generator - D&D magic item generator like in Diablo

DND-magic-item-Generator D&D magic item generator like in Diablo Legendary items

Mar 28, 2022

Nvote - Decentralized, vote-driven community similar to services like Reddit and HackerNews. Built on nostr

NVote Nvote is a decentralized, vote-driven community similar to services like R

Jan 4, 2023

Service that wrap up different movies-related APIs like IMDB and match it to streaming services

Service that wrap up different movies-related APIs like IMDB and match it to streaming services

Service that wrap up different movies-related APIs like IMDB and match it to streaming services. That way you can check in which platforms you can find your favorite movies.

Feb 10, 2022

Easy AWK-style text processing in Go

awk Description awk is a package for the Go programming language that provides an AWK-style text processing capability. The package facilitates splitt

Jul 25, 2022

This repo introduces a simple server, which provided some APIs for search DAS account's records or reverse records

Prerequisites Install Usage Others Das-Account-Indexer This repo introduces a simple server, which provided some APIs for search DAS account's records

Dec 13, 2022

Miller is like awk, sed, cut, join, and sort for name-indexed data such as CSV, TSV, and tabular JSON

Miller is like awk, sed, cut, join, and sort for name-indexed data such as CSV, TSV, and tabular JSON

What is Miller? Miller is like awk, sed, cut, join, and sort for data formats such as CSV, TSV, JSON, JSON Lines, and positionally-indexed. What can M

Jan 5, 2023

A POSIX-compliant AWK interpreter written in Go

GoAWK: an AWK interpreter written in Go AWK is a fascinating text-processing language, and somehow after reading the delightfully-terse The AWK Progra

Dec 31, 2022

It's so many regular expression forms are difficult to understand, such as perl, python, grep awk

Introduction Jamie Zawinski: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. It

Mar 31, 2022

I like reading news but I also like the terminal. I am leaning and practicing my go.

I like reading news but I also like the terminal. I am leaning and practicing my go.

I made an api and didn't know how to use it. Screenshots The initial screen when you first run the app. The screen after you specify an id. This app u

Jan 14, 2022

Fast, simple sklearn-like feature processing for Go

Fast, simple sklearn-like feature processing for Go

go-featureprocessing Fast, simple sklearn-like feature processing for Go Does not cross cgo boundary No memory allocation No reflection Convenient ser

Dec 2, 2022

GoBatch is a batch processing framework in Go like Spring Batch in Java

GoBatch is a batch processing framework in Go like Spring Batch in Java

GoBatch English|中文 GoBatch is a batch processing framework in Go like Spring Batch in Java. If you are familiar with Spring Batch, you will find GoBat

Dec 25, 2022

Secure storage for personal records built to comply with GDPR

Secure storage for personal records built to comply with GDPR

Databunker Databunker is a Personally Identifiable Information (PII) Data Storage Service built to Comply with GDPR and CCPA Privacy Requirements. Pro

Jan 8, 2023

dynflare is a tool to automatically update dns records at Cloudflare, when the ip changes.

dynflare dynflare is a tool to automatically update dns records at Cloudflare, when the ip changes. How it works The current ips are determined by ask

Dec 7, 2021

A command line tool for mainly exporting logbook records from Google Spreadsheet to PDF file in EASA format

A command line tool for mainly exporting logbook records from Google Spreadsheet to PDF file in EASA format

Logbook CLI This is a command line tool for mainly exporting logbook records from Google Spreadsheet to PDF file in EASA format. It also supports rend

Feb 6, 2022

Dynamically update DNS records with your interface's public IPs

Dynamically update DNS records with your interface's public IPs

Jul 2, 2022

Automatically register a list of domain names, add them to Cloudflare and set DNS records.

NameCannon Automatically register a list of domain names, add them as zones on Cloudflare, then add DNS records. Usage $ ./NameCannon --namesiloSecret

Jan 26, 2022
Comments
  • Prig compiled with Go 1.17 doesn't work when `go` command is 1.18

    Prig compiled with Go 1.17 doesn't work when `go` command is 1.18

    This is because importspkg.Process() returns an error due to the new generics syntax:

    $ prig -s -b 'fmt.Println(42)'
    main.go:214:10: expected '(', found '[' (and 2 more errors)
    func Sort[T int|float64|string](s []T, options ..._sortOption) []T {
             ^
    

    I guess we'll have to be conservative and make the generics test based on whether Prig itself is running with Go 1.18 or not.

It's so many regular expression forms are difficult to understand, such as perl, python, grep awk

Introduction Jamie Zawinski: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. It

Mar 31, 2022
GoBatch is a batch processing framework in Go like Spring Batch in Java
GoBatch is a batch processing framework in Go like Spring Batch in Java

GoBatch English|中文 GoBatch is a batch processing framework in Go like Spring Batch in Java. If you are familiar with Spring Batch, you will find GoBat

Dec 25, 2022
Dynamodb-expire-non-latest - Dynamodb spike to find best solution to set expire on old records

Goal, expire non-latest records User (identified by IP address), adds record A,

Jan 5, 2022
💾 Wolke API is the API behind Wolke image storage and processing aswell as user management

?? Wolke API Wolke API is the API behind Wolke image storage and processing aswell as user management Deploying To deploy Wolke Bot you'll need podman

Dec 21, 2021
Triangula-api-server - API server for processing images with Triangula

Triangula API server Minimalistic API server that calculates and serves artistic

Jan 10, 2022
Pokemon Unite scoreboard HUD and extra tools running over captured game feeds using the OpenCV video processing API and Client/Server architecture.
Pokemon Unite scoreboard HUD and extra tools running over captured game feeds using the OpenCV video processing API and Client/Server architecture.

unite Pokemon Unite scoreboard HUD and extra tools running over captured game feeds using the OpenCV video processing API. Client (OBS Live) Server Ar

Dec 5, 2022
Unofficial but convenient Go wrapper around the NVD API

NVD API The NVD API is an unofficial Go wrapper around the NVD API. Supports: CVE CPE How to use The following shows how to basically use the wrapper

Nov 1, 2021
`ls` but for your AWS VPC(s)
`ls` but for your AWS VPC(s)

lsvpc A simple AWS VPC listing tool to provide quick introspection on the makeup of a VPC One really cool use of this tool is to run: watch -c lsvpc -

Dec 30, 2021
fzf-like fuzzy-finder as a Go library
fzf-like fuzzy-finder as a Go library

go-fuzzyfinder go-fuzzyfinder is a Go library that provides fuzzy-finding with an fzf-like terminal user interface. Installation $ go get github.com/k

Jan 1, 2023
Software for archiving my digital stuff like tweets

rsms's memex Software for managing my digital information, like tweets. Usage First check out the source and build. You'll need Make and Go installed.

Nov 17, 2022