Skip to content

Instantly share code, notes, and snippets.

@7shi 7shi/gdict.sh
Last active May 11, 2020

Embed
What would you like to do?
[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
You can’t perform that action at this time.