haskell-awk: Transform text from the command-line using Haskell expressions.

[ apache, console, library, program ] [ Propose Tags ] [ Report a vulnerability ]

Hawk is a command line utility to process streams of text using Haskell code. It is intended to be used in a UNIX pipeline. It offers a configuration system to personalize imported modules and a way to represent values on the console.


[Skip to Readme]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 1.0, 1.0.1, 1.1, 1.1.1, 1.2, 1.2.0.1 (info)
Change log CHANGELOG.md
Dependencies base (>=4.13.0.0 && <4.16), bytestring (>=0.10.10.1), containers (>=0.6.2.1), directory (>=1.3.6.0), exceptions (>=0.10.4), extra (>=1.7.9), filelock (>=0.1.1.5), filepath (>=1.4.2.1), ghc (>=8.8.4), haskell-awk, haskell-src-exts (>=1.23.1), hint (>=0.9.0.3), list-t (>=1.0.4), mtl (>=2.2.2), process (>=1.6.9.0), stringsearch (>=0.3.6.6), template-haskell (>=2.15.0.0), time (>=1.9.3), transformers (>=0.5.6.2) [details]
License Apache-2.0
Author Mario Pastorelli <pastorelli.mario@gmail.com>, Samuel Gélineau <gelisam@gmail.com>
Maintainer Samuel Gélineau <gelisam@gmail.com>, Jens Petersen <juhpetersen@gmail.com>
Revised Revision 2 made by gelisam at 2024-02-09T05:45:56Z
Category Console
Home page https://github.com/gelisam/hawk#readme
Bug tracker https://github.com/gelisam/hawk/issues
Source repo head: git clone https://github.com/gelisam/hawk
Uploaded by gelisam at 2021-04-10T03:45:36Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Executables hawk
Downloads 4843 total (18 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for haskell-awk-1.2

[back to package description]

Hawk Hackage Build Status

Transform text from the command-line using Haskell expressions. Similar to awk, but using Haskell as the text-processing language.

Examples

In Unix the file /etc/passwd is used to keep track of every registered user in the system. Each entry in the file contains information about a single user, using a simple colon-separated format. For example:

root:x:0:0:root:/root:/bin/bash

The first field is the username. We can use Hawk to list all usernames as follows:

> cat /etc/passwd | hawk -d: -m 'head'
root

The -d option tells Hawk to use : as field delimiters, causing the first line to be interpreted as ["root", "x", "0", "0", "root", "/root", "/bin/bash"]. The -m tells Hawk to map a function over each line of the input. In this case, the function head extracts the first field of the line, which happens to be the username.

We could of course have achieved identical results by using awk instead of Hawk:

> cat /etc/passwd | awk -F: '{print $1}'
root

While Hawk and awk have similar use cases, the philosophy behind the two is very different. Awk uses a specialized language designed to concisely express many text transformations, while Hawk uses the general-purpose language Haskell, which is also known for being concise, among other things. There are many standard command-line tools that can be easily approximated using short Haskell expressions.

Another important difference is that while awk one-liners are self-contained, Hawk encourages the use of libraries and user-defined functions. By adding function definitions, module imports and language pragmas to Hawk's user-configurable prelude file, those functions, libraries and language extensions become available to Hawk one-liners. For instance, we could add a takeLast function extracting the last n elements from a list, and use it to (inefficiently) approximate tail:

> echo 'takeLast n = reverse . take n . reverse' >> ~/.hawk/prelude.hs
> seq 0 100 | hawk -a 'takeLast 3'
98
99
100

For more details, see the presentation and the documentation.

Installation

To install hawk, clone this repository, run stack install, and add ~/.local/bin to your PATH. Alternatively, you can also use cabal, but only v1-style is currently supported: run cabal v1-sandbox init && cabal v1-install, and add /.../.cabal-sandbox/bin to your PATH.

You should be ready to use Hawk:

> hawk '[1..3]'
1
2
3

The first run will create a default configuration file into ~/.hawk/prelude.hs if it doesn't exist.