Skip to content

Instantly share code, notes, and snippets.

@gosukiwi
Last active June 15, 2016 19:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gosukiwi/60710dc7f5c901e38683d57ce0286721 to your computer and use it in GitHub Desktop.
Save gosukiwi/60710dc7f5c901e38683d57ce0286721 to your computer and use it in GitHub Desktop.
Fuzzy Search

Usage: zf PATTERN [DIR]

Example: zf foo app/

Paste this somewhere. I use it in /usr/local/bin. Remeber to chmod +x /usr/local/bin/zf.

if [ -z "$1" ]
  then
    echo "usage: zf PATTERN [DIR]"
    echo "example: zf foo app/"
    exit 1
fi

regex=""
for (( i = 0; i < ${#1}; i++  )); do
  char=${1:$i:1}
  char="$char[^\n]*?"
  regex="$regex$char"
done
breakat=$(( ${#regex} - 7 ))
regex=${regex:0:breakat}

path=$2
if [ -z "$2" ] 
  then
    path="."
fi

grep -ERni --color=always "$regex" "$path"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment