SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
Haskell-awk

Haskell text processor for the commandline
Mario Pastorelli
Introduction
awk
a generic text processor where
“A file is treated as a sequence of records, and by
default each line is a record.” - Alfred V. Aho
developed in 1977 by Alfred Aho, Peter Weinberger, and Brian
Kernighan @ Bell Labs
uses AWK as programming language
ak'EI {pit"el Wrd"}
w BGN
rn Hlo ol! '

procedural
interpreted
a program is a series of pattern action pairs
Why another awk?
“Whenever faced with a problem, some people say
`Lets use AWK.' Now, they have two problems.” - D.
Tilbrook
avoid the AWK programming language
use a generic language, not a DSL
BGNslt" bcca,)frii abai]1r";o( i brr ";rn r
EI{pi(a
"a;o( n )[[]=;="fri n )=" ipit }
nb$wrs" bcca
u
od a
"

procedural (imperative) vs functional programming for stream
processing
Haskell-awk (Hawk)
a generic text processor where
“A stream is treated as a sequence of records, and
by default each line is a record.”
the same philosophy of awk!
developed in 2013 by me and Samuel Gélineau, the name is a tribute
to awk
uses Haskell as programming language
hw 'HloWrd"
ak "el ol!'

functional
(incrementally) compiled
a program is a Haskell expression
Why Haskell
expressive, clean and concise
>fle od[,,,]
itr d 1234
[,]
13

functions as composable building blocks
>ltwrCut=sm.mp(egh.wrs .lns
e odon
u
a lnt
od)
ie
>:yewrCut
tp odon
wrCut: Srn - It
odon : tig > n
>wrCut" 23n 56n 89
odon 1
4
7
"
9

partial application
>:yemp
tp a
mp: ( - b - []- []
a : a > ) > a > b
>:yent
tp o
nt: Bo - Bo
o : ol > ol
>:yempnt
tp a o
mpnt: [ol - [ol
a o : Bo] > Bo]
>mpnt[reFle
a o Tu,as]
[as,re
FleTu]

point-free style, laziness ...
Hawk
Modes
evaluate an expression
$hw ''
ak 1
1
$hw '12'
ak [,]
1
2
$hw '[,][,]'
ak [12,34]'
12
34

apply an expression to the input
$eh '2n'|hw - '.ees'
co 1n3
ak a Lrvre
3
2
1

map an expression to each record of the input
$eh ' 2n 4 |hw - '.ees'
co 1 3 '
ak m Lrvre
21
43
IO format
The input is, by default, a list of list of strings where lines are
separated by n and words by spaces
$eh ' 2n 4 |hw - 'hw
co 1 3 '
ak a so'
["""",""""]
[1,2][3,4]

Options -d/-D are provided to change delimiters or set them to
empty
$eh ',;,'|hw - -''-'''hw
co 1234
ak a d, D; so'
["""",""""]
[1,2][3,4]
$eh ' 2n 4 |hw - -' 'hw
co 1 3 '
ak a d' so'
[12,34]
" "" "
$eh ' 2n 4 |hw - -' -' 'hw
co 1 3 '
ak a d' D' so'
" 2n 4n
1 3 "

The output can be any type that instantiate the typeclass Rows
cas(hwa = Rw awee
ls So ) > os
hr
rp : BtSrn - a- [yetig
er : yetig >
> BtSrn]
Examples
get all users of a UNIX system
$ct/t/asd|hw -:- '.ed
a ecpsw
ak d m Lha'
ro
ot
deo
amn
..
.

select username and userid
$ct/t/asd|hw -:-'t - 'l- ( ! 0l! 2'
a ecpsw
ak d o' m  > l ! , ! )
ro
ot
0
deo 1
amn
..
.

sort by username (instead of pid)
$ct/t/asd|hw -:- '.oty(opr `n Lha)
a ecpsw
ak d a LsrB cmae o` .ed'
bnx22bn/i:bns
i::::i:bn/i/h
deo::::amn/s/bn/i/h
amnx11deo:ursi:bns
..
.

get the number of users using each shell

>ct/t/asd|hw -d '.a (.ed&&Llnt).Lgop.Lsr .LmpLls'
a ecpsw
ak a: Lmp Lha & .egh
.ru
.ot
.a .at
/i/ah1
bnbs:
..
.
Context
Hawk can be customized using files inside the context directory (by
default ~/.hawk)
The most important file is prelude.hs that contains the "runtime
context"
$ct~.akpeueh
a /hw/rld.s
{#LNUG EtneDfutue,OelaeSrns#}
- AGAE xeddealRls vroddtig ipr Peue
mot rld
ipr qaiidDt.yetigLz.hr a B
mot ulfe aaBtSrn.ayCa8 s
ipr qaiidDt.ita L
mot ulfe aaLs s

for instance, we can add a function for taking elements in an
interval
$eh 'aeewe se=Ltk ( -s .Ldo s > ~.akpeueh
co tkBten
.ae e
)
.rp ' > /hw/rld.s
$sq010|hw - 'aeewe 24
e
0
ak a tkBten
'
2
3
Implementation
Hawk must be fast
cache the context
use the timestamp to check if the context is changed since last
run
compile it with ghc
use locks to compile only once when multiple Hawk instances
instances are running
hw '1.'|hw - '.ae3
ak [.]
ak a Ltk '

use ByteString instead of String
...
Parse and interpret Haskell
Hawk combines two Haskell libraries
haskell-src-exts to deal with haskell source code
>ipr Lnug.akl.xsPre
mot agaeHselEt.asr
>gtoPams"- LNUG NIpiiPeueOelaeSrns#}n
eTprga {# AGAE omlctrld,vroddtig -"
Prek[agaerga(rLc
asO LnugPam Sco
{rFlnm ="nnw.s,scie=1 scoun=1)
scieae
ukonh" rLn
, rClm
}
[dn "omlctrld"Iet"vroddtig"]
Iet NIpiiPeue,dn OelaeSrns]

hint to interpret the user expression
>ipr Lnug.akl.nepee
mot agaeHselItrrtr
>rnnepee $stmot [Dt.n" > itrrt""(s: It
uItrrtr
eIprs "aaIt] > nepe 1 a : n)
Rgt1
ih
>rnnepee $stmot [Dt.n" > itrrt"o"(s: It
uItrrtr
eIprs "aaIt] > nepe fo a : n)
Lf (otopl [hErr{rMg="o i soe `o'})
et WnCmie Gcro ers
Nt n cp: fo"]
Thank you!
https://github.com/gelisam/hawk

Contenu connexe

Tendances

COSCUP2012: How to write a bash script like the python?
COSCUP2012: How to write a bash script like the python?COSCUP2012: How to write a bash script like the python?
COSCUP2012: How to write a bash script like the python?
Lloyd Huang
 
Bash shell
Bash shellBash shell
Bash shell
xylas121
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.ppt
Kalkey
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
Ohgyun Ahn
 
Unix Commands
Unix CommandsUnix Commands
Unix Commands
Dr.Ravi
 
Linux shell env
Linux shell envLinux shell env
Linux shell env
Rahul Pola
 
Unix Shell Scripting
Unix Shell ScriptingUnix Shell Scripting
Unix Shell Scripting
Mustafa Qasim
 

Tendances (20)

COSCUP2012: How to write a bash script like the python?
COSCUP2012: How to write a bash script like the python?COSCUP2012: How to write a bash script like the python?
COSCUP2012: How to write a bash script like the python?
 
Bash shell
Bash shellBash shell
Bash shell
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.ppt
 
OpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell Scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scripting
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
 
Scripting and the shell in LINUX
Scripting and the shell in LINUXScripting and the shell in LINUX
Scripting and the shell in LINUX
 
Unix Commands
Unix CommandsUnix Commands
Unix Commands
 
Complete Guide for Linux shell programming
Complete Guide for Linux shell programmingComplete Guide for Linux shell programming
Complete Guide for Linux shell programming
 
Linux shell env
Linux shell envLinux shell env
Linux shell env
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in Linux
 
Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!
 
Unix shell scripting tutorial
Unix shell scripting tutorialUnix shell scripting tutorial
Unix shell scripting tutorial
 
Shell script-sec
Shell script-secShell script-sec
Shell script-sec
 
How to admin
How to adminHow to admin
How to admin
 
Perl one-liners
Perl one-linersPerl one-liners
Perl one-liners
 
Unix Shell Scripting
Unix Shell ScriptingUnix Shell Scripting
Unix Shell Scripting
 

Similaire à Hawk presentation

DevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung FooDevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung Foo
brian_dailey
 

Similaire à Hawk presentation (20)

Shell programming
Shell programmingShell programming
Shell programming
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
 
Shell Scripting crash course.pdf
Shell Scripting crash course.pdfShell Scripting crash course.pdf
Shell Scripting crash course.pdf
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
 
Linux And perl
Linux And perlLinux And perl
Linux And perl
 
Unix
UnixUnix
Unix
 
Shellscripting
ShellscriptingShellscripting
Shellscripting
 
One-Liners to Rule Them All
One-Liners to Rule Them AllOne-Liners to Rule Them All
One-Liners to Rule Them All
 
Linux
LinuxLinux
Linux
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell Scripting
 
SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
Shell-Scripting-1.pdf
Shell-Scripting-1.pdfShell-Scripting-1.pdf
Shell-Scripting-1.pdf
 
Shell_Scripting.ppt
Shell_Scripting.pptShell_Scripting.ppt
Shell_Scripting.ppt
 
VTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERS
VTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERSVTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERS
VTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERS
 
DevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung FooDevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung Foo
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboy
 
First steps in C-Shell
First steps in C-ShellFirst steps in C-Shell
First steps in C-Shell
 
Commands and shell programming (3)
Commands and shell programming (3)Commands and shell programming (3)
Commands and shell programming (3)
 
Wildcards, Simple Shell Programs and Shell Variables
Wildcards, Simple Shell Programs and Shell VariablesWildcards, Simple Shell Programs and Shell Variables
Wildcards, Simple Shell Programs and Shell Variables
 
Linux com
Linux comLinux com
Linux com
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Dernier (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

Hawk presentation

  • 1. Haskell-awk Haskell text processor for the commandline Mario Pastorelli
  • 3. awk a generic text processor where “A file is treated as a sequence of records, and by default each line is a record.” - Alfred V. Aho developed in 1977 by Alfred Aho, Peter Weinberger, and Brian Kernighan @ Bell Labs uses AWK as programming language ak'EI {pit"el Wrd"} w BGN rn Hlo ol! ' procedural interpreted a program is a series of pattern action pairs
  • 4. Why another awk? “Whenever faced with a problem, some people say `Lets use AWK.' Now, they have two problems.” - D. Tilbrook avoid the AWK programming language use a generic language, not a DSL BGNslt" bcca,)frii abai]1r";o( i brr ";rn r EI{pi(a "a;o( n )[[]=;="fri n )=" ipit } nb$wrs" bcca u od a " procedural (imperative) vs functional programming for stream processing
  • 5. Haskell-awk (Hawk) a generic text processor where “A stream is treated as a sequence of records, and by default each line is a record.” the same philosophy of awk! developed in 2013 by me and Samuel Gélineau, the name is a tribute to awk uses Haskell as programming language hw 'HloWrd" ak "el ol!' functional (incrementally) compiled a program is a Haskell expression
  • 6. Why Haskell expressive, clean and concise >fle od[,,,] itr d 1234 [,] 13 functions as composable building blocks >ltwrCut=sm.mp(egh.wrs .lns e odon u a lnt od) ie >:yewrCut tp odon wrCut: Srn - It odon : tig > n >wrCut" 23n 56n 89 odon 1 4 7 " 9 partial application >:yemp tp a mp: ( - b - []- [] a : a > ) > a > b >:yent tp o nt: Bo - Bo o : ol > ol >:yempnt tp a o mpnt: [ol - [ol a o : Bo] > Bo] >mpnt[reFle a o Tu,as] [as,re FleTu] point-free style, laziness ...
  • 8. Modes evaluate an expression $hw '' ak 1 1 $hw '12' ak [,] 1 2 $hw '[,][,]' ak [12,34]' 12 34 apply an expression to the input $eh '2n'|hw - '.ees' co 1n3 ak a Lrvre 3 2 1 map an expression to each record of the input $eh ' 2n 4 |hw - '.ees' co 1 3 ' ak m Lrvre 21 43
  • 9. IO format The input is, by default, a list of list of strings where lines are separated by n and words by spaces $eh ' 2n 4 |hw - 'hw co 1 3 ' ak a so' ["""",""""] [1,2][3,4] Options -d/-D are provided to change delimiters or set them to empty $eh ',;,'|hw - -''-'''hw co 1234 ak a d, D; so' ["""",""""] [1,2][3,4] $eh ' 2n 4 |hw - -' 'hw co 1 3 ' ak a d' so' [12,34] " "" " $eh ' 2n 4 |hw - -' -' 'hw co 1 3 ' ak a d' D' so' " 2n 4n 1 3 " The output can be any type that instantiate the typeclass Rows cas(hwa = Rw awee ls So ) > os hr rp : BtSrn - a- [yetig er : yetig > > BtSrn]
  • 10. Examples get all users of a UNIX system $ct/t/asd|hw -:- '.ed a ecpsw ak d m Lha' ro ot deo amn .. . select username and userid $ct/t/asd|hw -:-'t - 'l- ( ! 0l! 2' a ecpsw ak d o' m > l ! , ! ) ro ot 0 deo 1 amn .. . sort by username (instead of pid) $ct/t/asd|hw -:- '.oty(opr `n Lha) a ecpsw ak d a LsrB cmae o` .ed' bnx22bn/i:bns i::::i:bn/i/h deo::::amn/s/bn/i/h amnx11deo:ursi:bns .. . get the number of users using each shell >ct/t/asd|hw -d '.a (.ed&&Llnt).Lgop.Lsr .LmpLls' a ecpsw ak a: Lmp Lha & .egh .ru .ot .a .at /i/ah1 bnbs: .. .
  • 11. Context Hawk can be customized using files inside the context directory (by default ~/.hawk) The most important file is prelude.hs that contains the "runtime context" $ct~.akpeueh a /hw/rld.s {#LNUG EtneDfutue,OelaeSrns#} - AGAE xeddealRls vroddtig ipr Peue mot rld ipr qaiidDt.yetigLz.hr a B mot ulfe aaBtSrn.ayCa8 s ipr qaiidDt.ita L mot ulfe aaLs s for instance, we can add a function for taking elements in an interval $eh 'aeewe se=Ltk ( -s .Ldo s > ~.akpeueh co tkBten .ae e ) .rp ' > /hw/rld.s $sq010|hw - 'aeewe 24 e 0 ak a tkBten ' 2 3
  • 13. Hawk must be fast cache the context use the timestamp to check if the context is changed since last run compile it with ghc use locks to compile only once when multiple Hawk instances instances are running hw '1.'|hw - '.ae3 ak [.] ak a Ltk ' use ByteString instead of String ...
  • 14. Parse and interpret Haskell Hawk combines two Haskell libraries haskell-src-exts to deal with haskell source code >ipr Lnug.akl.xsPre mot agaeHselEt.asr >gtoPams"- LNUG NIpiiPeueOelaeSrns#}n eTprga {# AGAE omlctrld,vroddtig -" Prek[agaerga(rLc asO LnugPam Sco {rFlnm ="nnw.s,scie=1 scoun=1) scieae ukonh" rLn , rClm } [dn "omlctrld"Iet"vroddtig"] Iet NIpiiPeue,dn OelaeSrns] hint to interpret the user expression >ipr Lnug.akl.nepee mot agaeHselItrrtr >rnnepee $stmot [Dt.n" > itrrt""(s: It uItrrtr eIprs "aaIt] > nepe 1 a : n) Rgt1 ih >rnnepee $stmot [Dt.n" > itrrt"o"(s: It uItrrtr eIprs "aaIt] > nepe fo a : n) Lf (otopl [hErr{rMg="o i soe `o'}) et WnCmie Gcro ers Nt n cp: fo"]