-
-
Save derekbrokeit/1119737 to your computer and use it in GitHub Desktop.
A script to grep through an Anki deck
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 | |
# | |
# Search for a term within an Anki deck. | |
# | |
# Usage: grepanki.sh someword | |
# run "grepanki" without any input for more detailed help | |
# | |
# You might want to add special aliases to the .bashrc or .bash_profile files | |
# alias gpj='grepanki' # searches defaultly | |
# alias gpr='grepanki -r' # searches the reverse direction | |
# alias gps='grepanki -prim Examples' # finds all words where example sentences contain the query | |
# | |
# Also, make sure this file is executable: | |
# chmod +x grepanki | |
# | |
# Lastly, change displayResult to display the final printing procedure to whatever your prefer | |
## default options | |
deck="$HOME/Documents/Anki/日本語.anki" # the deck being searched | |
primarySearchField="Meaning" # the name of the sarch field | |
addField="Expression" # additional field to display [Note: this is the desired field] | |
dontParseCards="文法" # this is a string present in the name of a | |
# card model I don't want to search | |
## internal functions | |
# arranges the display format | |
function displayResult() { | |
primaryResult=$1 | |
addResult=$2 | |
echo "$addResult" | |
echo "$primaryResult" | |
} | |
# displays help material | |
function helpList() { | |
echo "${0##*/}: $@" | |
echo " available options:" | |
echo " usage: ${0##*/} {[option] [value]} [searchTerm]" | |
echo " OPTIONS: explanation:" | |
echo " -r reverse -prim and -add fields [next value must be searchTerm]" | |
echo " -h or --help repeats this help text [no value given]" | |
echo " -deck .anki deck file to search" | |
echo " -prim the field where the search term is found" | |
echo " -add the additional field that accompanies the search field" | |
echo " -exclude exclude cards that have this within the name" | |
echo " default behavior:" | |
echo " usage: ${0##*/} [searchTerm]" | |
echo " -deck $deck" | |
echo " -prim $primarySearchField" | |
echo " -add $addField" | |
echo " -exclude $dontParseCards" | |
} | |
## check input arguments | |
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]] || [[ "$#" -eq 0 ]] ; then | |
helpList | |
exit | |
elif [[ "$#" -gt 1 ]] ; then | |
# detected input options | |
while [ $# -gt 1 ] ; do | |
# final input argument MUST be the searchTerm | |
option=$1 | |
value=$2 | |
shift 2 | |
case $option in | |
-r ) | |
# reverse search | |
$0 -prim $addField -add $primarySearchField $value | |
exit | |
;; | |
-deck ) | |
# select deck | |
deck=$value | |
;; | |
-prim ) | |
# change primary search field | |
primarySearchField=$value | |
;; | |
-add ) | |
# change additional display field | |
addField=$value | |
;; | |
-exclude ) | |
# change excluded card | |
dontParseCards=$value | |
;; | |
* ) | |
# unkown input options | |
echo "${0##*/}" | |
echo "unkown option: \"$option\" value: \"$value\"" | |
echo | |
helpList | |
exit | |
;; | |
esac | |
done | |
fi | |
## begin searching | |
searchTerm=$1 # the incoming search term | |
# get all relevant card modles | |
fieldModelIds=$(sqlite3 $deck 'select id from fieldModels where (name="'"$primarySearchField"'") order by modelId') | |
addFieldModelIds=$(sqlite3 $deck 'select id from fieldModels where (name="'"$addField"'") order by modelId') | |
# begin searching the database | |
i=0 | |
for fModelId in $fieldModelIds #$fieldModelIds | |
do | |
let i++ | |
# get rid of fieldModelIds that come from cards I don't need | |
parseIfNotPresent=$(sqlite3 $deck 'select name from cardModels where modelId=(select modelId from fieldModels where id='"$fModelId"')' | grep $dontParseCards) | |
if [ "x${parseIfNotPresent}" == "x" ] ; then | |
# produce the primary-search-result and factId | |
primaryFieldVals=$(sqlite3 $deck 'select value,factId from fields where fieldModelId='$fModelId' order by factId' | grep "$searchTerm") | |
# separate them | |
primSearchResult=$(echo "$primaryFieldVals" | awk -F\| '{print $1}') | |
factIds=$(echo "$primaryFieldVals" | awk -F\| '{print $2}') | |
j=0 | |
for fact in $factIds | |
do | |
let j++ | |
# get primary result | |
primRes=$(echo "$primSearchResult" | sed -n ${j}'p') | |
# get add result | |
add=$(echo $addFieldModelIds | awk '{print $'$i'}') | |
addRes=$(sqlite3 $deck 'select value from fields where factid='$fact' and fieldModelId='$add) | |
displayResult "$primRes" "$addRes" | |
echo "" | |
done | |
# else | |
# echo "didnt parse: $fModelId" | |
fi | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment