1
1
mirror of https://github.com/gelisam/hawk.git synced 2024-11-30 11:52:00 +03:00
Haskell text processor for the command-line
Go to file
2024-02-09 22:51:16 -05:00
.github/workflows stack: bump oldest to lts10 2022-04-17 12:45:00 +08:00
doc clarify first run sentence 2016-07-15 11:23:18 -04:00
runtime/System/Console/Hawk only use Paths_haskell_awk from the library 2021-01-01 16:33:05 -05:00
src Location: Semigroup deprecated Option in ghc-9.0 2021-07-03 17:11:14 +08:00
tests TestUtils: silence <$> import warning for base >= 4.8 2021-07-03 13:44:17 +08:00
.gitignore switch to hpack 2018-03-17 17:44:53 -04:00
bump-lower-bounds.vim bump oldest supported lts 2021-01-01 15:41:44 -05:00
CHANGELOG.md prepare 1.2.0.1 2021-05-08 11:11:52 +08:00
CONTRIBUTING.md github prefers "good first issue" to "easy" 2020-06-16 11:04:33 -04:00
haskell-awk.cabal bump lower bounds to match what CI tests 2024-02-09 22:51:16 -05:00
LICENSE change to Apache v2 license 2013-09-04 21:54:04 +02:00
NOTICE change to Apache v2 license 2013-09-04 21:54:04 +02:00
oldest-supported-lts.yaml stack: bump oldest to lts10 2022-04-17 12:45:00 +08:00
package.yaml bump lower bounds to match what CI tests 2024-02-09 22:51:16 -05:00
README.md update README.md github src repo links to use main instead of master 2021-06-30 01:08:33 +08:00
Setup.hs switch to cabal-doctest 2021-01-01 14:45:39 -05:00
stack.yaml stack: update to lts-17.15 2021-07-03 11:12:25 +08:00

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, either run stack install haskell-awk or cabal install haskell-awk.

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.