Last active
May 11, 2020 10:53
-
-
Save 7shi/32485d6e0cbccffab4fc321414dc52b8 to your computer and use it in GitHub Desktop.
[sh] grep the text dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
CMDNAME=`basename $0` | |
INSERT="✔ " | |
DICT="" | |
MAX="50" | |
WORD="0" | |
OPTS="-i" | |
COLOR="--color=always" | |
function usage () { | |
echo "usage: $CMDNAME -d DICT [-c] [-m max] [-w] words ..." 1>&2 | |
exit 1 | |
} | |
while getopts d:cm:w OPT; do | |
case $OPT in | |
"d" ) DICT="$OPTARG";; | |
"c" ) COLOR="--color=never";; | |
"m" ) MAX="$OPTARG";; | |
"w" ) WORD="1";; | |
* ) usage;; | |
esac | |
done | |
shift `expr $OPTIND - 1` | |
if [ $# -lt 1 ]; then usage; fi | |
if [ "A$DICT" = "A" ]; then | |
echo "Please specify the dictionary by -d." 1>&2 | |
exit 1 | |
fi | |
WORDS="" | |
SPC="" | |
for ARG in $@; do | |
WORDS+="$SPC$ARG" | |
SPC=" " | |
done | |
if [ $WORD -eq 1 ]; then WORDS="\b$WORDS\b"; fi | |
HEAD="head -n $MAX" | |
if [ $MAX -lt 1 ]; then HEAD="cat"; fi | |
grep $OPTS $COLOR "$WORDS" $DICT | $HEAD | sed "s/^/$INSERT/" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment