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
- *uniq* - remove duplicated lines:
- `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.
- *head* - print the first few lines of a file:
- `eddie -l -L "unlines . take 10"`
- *tail* - print the last 10 lines of a file:
- `eddie -l -L "unlines . (drop =<< subtract 10 . length)"`
- *wc* - count words (or lines, or characters) in a file:
- count characters with `eddie tshow.length`
count words with `eddie tshow.length.words`
count lines with `eddie tshow.length.lines` or `eddie -l -L tshow.length`
- *wc with names* - counts as above, but adding the file name.
- count characters with `eddie 'tshow . second length=`
count words with `'eddie tshow . second (length . words)'`
count lines with `'eddie tshow . second (length . lines)'`
- *fgrep* - select lines containing a substring:
- `eddie -l -L 'unlines . filter (isInfixOf "substring")'`
- *grep* - select lines matching a regular expression:
- `eddie -l -L -m Text.Regex.TDFA -m Text.Regex.TDFA.Text 'unlines . (filter (=~ ("r.e." :: Text)))'`
- *sort* - sort the lines in a file:
- `eddie -l -L unlines.sort`
### 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.)
- Repeat a file
- `eddie cycle`
- Print a file double spaced
- `eddie -l -L 'unlines . intersperse ""'`
- Strip blank lines
- `eddie -l -L 'unlines . filter (not . null)'`
- Map a file to upper case
- `eddie toUpper`
- Strip whitespace from lines in a variety of ways
- From the start of each line: `eddie -l -m Data.Char 'dropWhile isSpace'`
From the end of each line: `eddie -l -m Data.Char 'reverse . dropWhile isSpace . reverse'`
From both ends of a line: `eddie -l -m Data.Char 'let f = reverse . dropWhile isSpace in f . f'`
- Indent all the lines in a file
- `eddie -l '(" " ++)'`
Z b58f8c4557b54cff1740ed0fefe80860