QuickQuestion
QuickQuestion copied to clipboard
Alternative search command for Linux: ack/grep + find instead of mdfind?
Hi Brett,
There are probably many Linux users interested in using qq. Hence a suggestion/tiny please: would you mind adding an alternative search command that utilizes ack
or grep
instead of mdfind
?
The corresponding line could simply be added to the shell script, but it could be commented out by default.
Thank you and all the best from Estonia, Mart
A minor update: I'm pretty much an end-user, but I came up with this:
ANSWER=`find "$NOTESDIR" -name "??*.txt" -exec grep "$INPUT" {} \; | head -n 1`
But it's not working properly, apparently due to some issues with cat
-- here's my log. As you can see, one of the "answer" lines is also printed twice for some reason:
[mart@arch]$ ./qq -a "question one" "answer"
Question added and answered.
[mart@arch]$ ./qq answer
Q: answer?
A: cat: answer: No such file or directory
cat: answer: No such file or directory
cat: answer: No such file or directory
I also tried this:
ANSWER=`grep -r --include="??*.txt" "$INPUT" $NOTESDIR | head -n 1`
but that returned "Sorry, I don't know the answer..." to every query.
Due to the need for regular sudo updatedb
etc, the script won't be as responsive on Linux as it is on OSX. But can you please help? Thanks!
From TJ Luoma:
#!/bin/bash
# notes folder, for note creation and limiting searches
NOTESDIR="/Users/luomat/Dropbox/txt"
# extension used for your notes
NOTESEXT="md"
# the prefix you use to separate "Question" notes
NOTESPRE="@qq"
# editor command to use for modifying answers
EDITOR="bbedit"
NOTESDIR=`echo $NOTESDIR|sed -e '/\/$/! s/$/\//'`
if [[ "$1" == "-h" ]]
then
appname=`basename $0`
echo "$appname: build a knowledgebase with plain text files"
echo "Find an answer: $appname \"terms to search for\""
echo "Add a question\answer: $appname -a \"Question in natural language\" \"Succinct answer\""
echo "Add a question\answer interactively: $appname -a"
echo "Edit a question\answer: $appname -e \"terms to search for\" # first question found is edited"
elif [[ "$1" == "-a" ]]
then
if [ $# == 3 ]; then
QUESTION=$2
ANSWER=$3
elif [ $# == 1 ]; then
echo -n "Question: "
read QUESTION
echo -n "Answer: "
read ANSWER
else
echo "Invalid number of arguments for -a(dd). Requires question and answer (or no arguments to input them at runtime)."
echo "example: ${0##*/} -a \"What is the meaning of life?\" \"42\""
exit 1
fi
echo -n "$ANSWER" > "${NOTESDIR}$NOTESPRE $QUESTION.$NOTESEXT" && echo "Question added and answered." || echo "Something went wrong"
elif [[ "$1" == "-e" ]]; then
shift
INPUT=$@
ANSWER=`find "${NOTESDIR}" -iname \*.${NOTESEXT} -iname ${NOTESPRE}\* -exec fgrep -il "$INPUT" {} \;`
if [[ "$ANSWER" == "" ]]; then
echo "No results found for search."
exit 2
else
$EDITOR "$ANSWER"
fi
else
INPUT=$@
echo "`find ${NOTESDIR} -iname \*.${NOTESEXT} -iname ${NOTESPRE}\* -exec fgrep -il $INPUT {} \;`"|while read LINE; do
if [[ "$LINE" == "" ]]; then
echo "Sorry, I don't know the answer to that question."
exit 1;
fi
QUESTION=${LINE##*/}
echo -n "Q: "
NOTESPREESC=`echo "$NOTESPRE"|sed -E 's/([\?\!\$\`\"]) ?/\\\\\1/g'`
echo ${QUESTION%%.$NOTESEXT}|sed -E "s/$NOTESPREESC ?//g"|sed -E 's/(.)$/\1?/'
echo -n "A: "
cat "$LINE"|sed -E 's/@\([^\)]+\) ?//g'|sed -E 's/@copy\(([^)]*)\)/\1/'|sed -E 's/@open\(([^)]*)\)/Related URL: \1/'|sed -E 's/^[ ]*|[ ]*$//g'
if [[ `cat "$LINE"|grep -E '@copy\('` ]]; then
cat "$LINE"|grep '@copy('|sed -E 's/.*@copy\(([^)]*)\).*/\1/'|tr -d '\n'|pbcopy
echo "Example in clipboard"
fi
if [[ `cat "$LINE"|grep -E '@open\('` ]]; then
url=$(cat "$LINE"|grep '@open('|sed -E 's/.*@open\(([^)]*)\).*/\1 /'|tr -d '\n')
open -g $url
echo "Opened URL"
fi
echo
done
fi
exit 0
You made my day. Thanks very much!