Skip to content

Instantly share code, notes, and snippets.

@ryo1kato
ryo1kato / hmlgrep-fgrep.hs
Created September 2, 2014 23:17
fgrep with regex support for --rs
--
-- hmlgrep - Haskell Multi-Line Grep
--
{-
TODOs:
* FIX: '-' and '--' handling in optparse-applicative
* https://github.com/pcapriotti/optparse-applicative/pull/99
* Use Cabal for build?
* Use Boyer-Moore for non-regex patterns using stringsearch library:
dropLast n bstr = BS.take (BS.length bstr - n) bstr
takeLast n bstr = BS.drop (BS.length bstr - n) bstr
reComp pat = makeRegexOpts compBlank execBlank pat
breakNextRegex re bstr =
if pos >= 0
then BS.splitAt pos bstr
else (bstr, BS.empty)
where (pos, len) = bstr =~ re :: (MatchOffset,MatchLength)
afterLastRegexOf :: ByteStr -> ByteStr -> ByteStr
afterLastRegexOf pat bstr = revSearchRE 0 bstr
where
re = makeRegexOpts compBlank execBlank pat
revSearchRE n s
| BS.null s = bstr
|_
| otherwise = revSearchRE (n+lastlinelen) ()
where
lastline = afterLastRegexOf (pack "\n") s
import Control.Monad
import Data.Maybe
regexChars = "^$(|)[]{}.*"
regexCharsLast = "$)]}.*"
toPlainString' :: String -> String -> Maybe String
toPlainString' s [] = Just s
toPlainString' s (r:[]) = if r `elem` regexCharsLast
then Nothing
<html>
<script TYPE="text/javascript" LANGUAGE="javascript">
window.resizeTo(100,200);
</script>
</html>
--
-- hmlgrep - Haskell Multi-Line Grep
--
{-
TODOs:
* Show filenames with --count option for multiple file input
* Implement match highlight
* FIX: '-' and '--' handling in optparse-applicative
* https://github.com/pcapriotti/optparse-applicative/pull/99
* Use Boyer-Moore for non-regex patterns using stringsearch library:
@ryo1kato
ryo1kato / highlightRange.hs
Created August 15, 2014 15:32
highlightRange
import System.Console.ANSI
import qualified Data.ByteString.Lazy.Char8 as BS
import Text.Regex.PCRE
import Text.Regex.PCRE.ByteString
import Data.Int
hlCode = setSGRCode [SetColor Foreground Vivid Red]
hlReset = setSGRCode [Reset]
d = map BS.pack [
cat result | while read t p; do t1=${t%-*}; t2=${t#*-}; p1=${p%-*}; p2=${t#*-}; t+=(t1 $t2); if (( p1 > p2 )); then echo ${t1} win; echo ${t2} lose; else echo ${t2} win; echo ${t1} lose; fi; done | sort | uniq -c
#!/bin/bash
n=$(<"$1")
nums=($n)
i=$(sort -nr $1|head -1)
while ((i--))
do
for num in "${nums[@]}"
do
if [ $i -gt $num ]; then
echo -n " "
curl http://ekikara.jp/newdata/line/1301351.htm | nkf -u | grep '<span class="l"><a href="../station/[0-9]*.htm">' | sed -e 's/<[^>]*>//g' | tr -d ' ' | cut -d '(' -f 1