-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alternative search command for Linux: ack/grep + find instead of mdfind? #3
Comments
A minor update: I'm pretty much an end-user, but I came up with this:
But it's not working properly, apparently due to some issues with
I also tried this:
but that returned "Sorry, I don't know the answer..." to every query. Due to the need for regular |
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! |
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
orgrep
instead ofmdfind
?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
The text was updated successfully, but these errors were encountered: