/usr/bin/edict-grep is in edict 2016.12.06-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | #!/bin/bash
set -e
common=n
word=n
exact=n
case_sensitive=n
encoding="$(locale -k charmap|sed -e 's/.*="\([^"]*\)"/\1/')"
edict=/usr/share/edict/edict
function usage()
{
cat <<EOF
Usage: $(basename $0) [options] <query>...
Options:
-c: Match only common entries.
-w: Word match; each English query must match whole words only.
-e: Exact match; each query must match an entire edict field.
-s: Make English queries case-sensitive (default is insensitive).
-f EDICT_FILE: Path to edict (default: $edict).
This program assumes edict is in the original EUC-JP encoding.
-c Current terminal character encoding (default: $encoding).
-h: Show this message.
If you use multiple queries, it is like an AND search (that is,
\$ $(basename $0) cold war
will match all entries mentioning both 'cold' and 'war'. To
search for an exact sentence, use shell escaping:
\$ $(basename $0) "cold war"
(and consider -e).
Be sure to put the queries only AFTER all options!
EOF
}
while getopts "cpwesf:c:h" opt; do
case $opt in
c|p) common=y;;
w) word=y;;
e) exact=y;;
s) case_sensitive=s;;
f) edict="$OPTARG";;
c) encoding="$OPTARG";;
h) usage; exit 0;;
*) usage; exit 1;;
esac
done
shift $(($OPTIND -1))
if ! [ "$encoding" ]; then
cat 1>&2 <<EOS
Error: could not guess terminal character encoding from \`locale -k charmap\`.
(You can use option -c as a workaround.)
EOS
fi
if ! [ -r "$edict" ]; then
echo "Error: could not read edict file \`$edict'." 2>&1
echo "Use option -f to specify the dictionary file." 2>&1
exit 1
fi
function encode()
{
iconv -f $encoding -t EUC-JP
}
function decode()
{
iconv -f EUC-JP -t $encoding
}
tmp1=$(mktemp)
tmp2=$(mktemp)
function cleanup()
{
rm -f $tmp1 $tmp2 2>&1
}
trap cleanup EXIT
declare -a queries
for arg in "$@"; do
regexp="$arg"
if [ $word == y ]; then
regexp="\<$regexp\>"
fi
if [ $exact == y ]; then
# catches kanji words at start of line
tmp="(^$regexp )"
# catches pronounciations (between brackets)
tmp+="|(\\[$regexp\\])"
# catches exact english queries, ignoring initial dictionary tags
# that are between parenthesis
tmp+="|(/(\\([^)]*\\) *)*$regexp)/"
regexp="$tmp"
fi
queries+=("$regexp")
done
grepopts='-E'
if ! [ $case_sensitive == y ]; then
grepopts+=' -i'
fi
first=y
for query in "${queries[@]}"; do
equery="$(echo "$query" | encode)"
if [ $first == y ]; then
first=n
grep $grepopts "$equery" "$edict" > $tmp1
else
grep $grepopts "$equery" $tmp1 > $tmp2 && cp $tmp2 $tmp1
fi
done
if [ $common == y ]; then
common_rg="$(echo '(P)' |encode)"
grep $grepopts "$common_rg" $tmp1 > $tmp2 && cp $tmp2 $tmp1
fi
# a last grep to color the matched parts
color_regexp=''
for query in "${queries[@]}"; do
color_regexp+="|($query)"
done
color_regexp="$(echo "$color_regexp"|encode)"
grep $grepopts --color=yes "$color_regexp" $tmp1|decode
cleanup
|