eddie
Artifact [af6deefe92]
Not logged in
Public Repositories
mwm's Repositories

Artifact af6deefe922839e4ca532e4ad2ba97540186c14b:

Wiki page [UnixCommands] by mwm 2015-05-23 00:36:07.
D 2015-05-23T00:36:07.971
L UnixCommands
N text/x-markdown
P 7490172b967d779cf9dd2f900b67c1f554cdbe59
U mwm
W 2553
#How to implement various Unix commands with eddie.

## Introduction

This is just a list of unix commands (or things that feel like they should be Unix commands) along with a partial implementation of them using eddie.

## The Unix Bestiary

<dl>
<dt>*uniq* - remove duplicated lines:</dt>
<dd>`eddie -l -L unlines.hashNub` will remove all dupliciates. `eddie -L -l "unlines . map headEx . group"` only works on presorted files, but works in *O*(n) time.</dd>

<dt>*head* - print the first few lines of a file:</dt>
<dd>`eddie -l -L "unlines . take 10"`</dd>

<dt>*tail* - print the last 10 lines of a file:</dt>
<dd>`eddie -l -L  "unlines . (drop =<< subtract 10 . length)"`</dd>

<dt>*wc* - count words (or lines, or characters) in a file:</dt>
<dd>count characters with `eddie tshow.length`<br />
count words with `eddie tshow.length.words`<br />
count lines with `eddie tshow.length.lines` or `eddie -l -L tshow.length`</dd>

<dt>*wc with names* - counts as above, but adding the file name.</dt>
<dd>count characters with `eddie 'tshow . second length=`<br />
count words with `'eddie tshow . second (length . words)'`<br />
count lines with `'eddie tshow . second (length . lines)'`</dd>


<dt>*fgrep* - select lines containing a substring:</dt>
<dd>`eddie -l -L 'unlines . filter (isInfixOf "substring")'`</dd>

<dt>*grep* - select lines matching a regular expression:</dt>
<dd>`eddie -l -L -m Text.Regex.TDFA -m Text.Regex.TDFA.Text 'unlines . (filter (=~ ("r.e." :: Text)))'`</dd>

<dt>*sort* - sort the lines in a file:</dt>
<dd>`eddie -l -L unlines.sort`</dd>

### The Alternative Bestiary

A collection of commands that do unix-like file processing but don't correspond to a generaly available command (borrowing heavily from http://www.haskell.org/haskellwiki/Simple_unix_tools.)

<dl>
<dt>Repeat a file</dt>
<dd>`eddie cycle`</dd>

<dt>Print a file double spaced</dt>
<dd>`eddie -l -L 'unlines . intersperse ""'`</dd>

<dt>Strip blank lines</dt>
<dd>`eddie -l -L  'unlines . filter (not . null)'`</dd>

<dt>Map a file to upper case</dt>
<dd>`eddie toUpper`</dd>

<dt>Strip whitespace from lines in a variety of ways</dt>
<dd>From the start of each line: `eddie -l -m Data.Char 'dropWhile isSpace'`<br />
From the end of each line: `eddie -l -m Data.Char 'reverse . dropWhile isSpace . reverse'`<br />
From both ends of a line: `eddie -l -m Data.Char 'let f = reverse . dropWhile isSpace in f . f'`</dd>

<dt>Indent all the lines in a file</dt>
<dd>`eddie -l '("     " ++)'`</dd>
Z b58f8c4557b54cff1740ed0fefe80860