Skip to content

Instantly share code, notes, and snippets.

@vasanthela
Created November 3, 2013 20:45
Show Gist options
  • Save vasanthela/7294606 to your computer and use it in GitHub Desktop.
Save vasanthela/7294606 to your computer and use it in GitHub Desktop.
Search and display list of local files by keywords
#!/usr/bin/env bash
# Setup colors
NONE='\033[00m'
GREEN='\033[00;32m'
YELLOW='\033[00;33m'
# Example usage
if (($# == 0)); then
echo "Usage: ./search.sh keyword1 [keyword] ..."
exit
fi
echo -e Searching ${YELLOW} "$@" ${NONE}
if (($# == 1)); then
# Search using ack
# pass in all args to ack, and list only files
FILES=`ack -l $@`
else
# Build a multiword ack search that should result
# in CMD equaling someting like
# grep -f <(ack -l foo) <(ack -l bar)
CMD="grep -f"
for var in "$@"; do
CMD="$CMD <(ack -l $var)"
done
# debug CMD variable
# echo "$CMD"
# echo ".."
# Things I tried to get eval saved into variable FILES
# FILES=`grep -f "$ACKS"`
# FILES=`grep -f '$ACKS'`
# FILES=`"$CMD"`
# FILES=$($CMD)
# FILES=$("$CMD")
# FILES=($("$CMD"))
# FILES=$((CMD))
# FILES=${$CMD}
# eval "FILES=\$(( $ACKS ))"
# eval \${$CMD}
# FILES= eval "$CMD"
# eval "FILES=$CMD"
# FILES= $("$CMD")
# F="FILES"
# eval $F="$CMD
# echo `"$CMD"`
eval "$CMD"
exit # Exit because eval will output file list
# Avoid exit after figuring out how to save result
# of eval to variable FILES
fi
for f in $FILES
do
FILENAME=${GREEN}$f${NONE} # output filename in green
FILE_SUMMARY=`sed -n '3,3p' $f` # output only line 3, assumed to be summary line of file
echo -e $FILENAME " " $FILE_SUMMARY
done
tput sgr0 # reset ANSI colors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment