Skip to content

Instantly share code, notes, and snippets.

@7shi
Last active May 11, 2020 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 7shi/32485d6e0cbccffab4fc321414dc52b8 to your computer and use it in GitHub Desktop.
Save 7shi/32485d6e0cbccffab4fc321414dc52b8 to your computer and use it in GitHub Desktop.
[sh] grep the text dictionary
#!/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