Ivor O’Connor

March 21, 2009

My Find Utility

Filed under: Uncategorized — ioconnor @ 9:44 pm

I tend to get customers that I can’t hear correctly over the phone. Sometimes I hear ‘b’ instead of ‘v’ or transpose a character. Having a search utility that overlooks my small typos is handy. The following finds an exact match when there is one, which is nice when there is an exact match, but more importantly it finds the files that are meant. Not necessarily what it was exactly told to fetch. There can still be a few holes. For instance when it matches a typo to an exact file. However this version gives a few clues as what could be wrong. This is done by showing the term with the fuzziness in parens followed by how many files matched with how many of those are in the global set of matching files. Perhaps a term needs to be changed…

It gives output like this on the command line plus it opens the files up with gvim in another window

:~$ f thomas jefferson
:TERM: thomas(0) 53 53
:TERM: jefferson(0) 23 1
/customers/customer_4855.shtml

real 0m1.917s
user 0m1.248s
sys 0m0.312s
:~$

The following is the actual code. It could be cleaned up a bit but I really do not like bash scripting and so find it an incredibly odious task. I must remind myself if it is over 10 lines of code to write a C++ piece of code. Grrrrrrrr


#!/bin/bash
####################################################################################################
# DESCRIPTION
# Searches through a directory looking for files containing all of fuzzy search terms specified
# Very handy in avoiding duplicating customer records because of slight mispellings
#
# EXAMPLE
# f xxx yyy zzz
#
# TODO
# 1
#
# BUGS
#
####################################################################################################
# GLOBALS
#===================================================================================================
G_FileList=""
G_Fuzz=""

####################################################################################################
#===================================================================================================
function printStatus() {
echo ":TERM: " $1"("$G_Fuzz")" $(echo $G_FileList | wc -w) $(echo "$2" | wc -w)
}

####################################################################################################
#===================================================================================================
function getFileList() {
searchTerm=$1
G_FileList=""
for (( G_Fuzz=0; G_Fuzz<3; G_Fuzz++ )); do
G_FileList=$(agrep -$G_Fuzz -i $searchTerm /customers/*.*html /customers/*.php | awk -F":" '{ print $1 }' | sort -n | uniq );
if [[ 0 != $(echo $G_FileList | wc -w) ]]; then
break
fi
done
}

####################################################################################################
#===================================================================================================
function main() {
getFileList $1
MasterFileList=$G_FileList
printStatus $1 "$MasterFileList"
shift
for var in "$@"; do
if [[ 0 != $(echo $MasterFileList | wc -w) ]]; then
getFileList $var
MasterFileList=$(echo $MasterFileList $G_FileList | tr " " "\n" | sort | uniq -d )
printStatus $var "$MasterFileList"
else
echo "No files contain the search terms"
break
fi
done
if [[ 0 != $(echo $MasterFileList | wc -w) ]]; then :
echo $MasterFileList | tr " " "\n" | head
gvim +/$1 $MasterFileList
fi
}

time main "$@"

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: