• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

gelisam/hawk: Haskell text processor for the command-line

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

gelisam/hawk

开源软件地址(OpenSource Url):

https://github.com/gelisam/hawk

开源编程语言(OpenSource Language):

Haskell 99.8%

开源软件介绍(OpenSource Introduction):

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.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
pcapriotti/optparse-applicative: Applicative option parser发布时间:2022-06-24
下一篇:
qfpl/hpython: Haskell language tools for Python发布时间:2022-06-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap