I have to read a lot of code and search through many code bases. I frequently find myself using something like grep -r --include=.C "test string" .
to find files containing a certain string or regular expression. However, to simplify this and expedite the process, I want to create a function that allows me to specify what I am looking for, what are acceptable file extensions and what are inacceptable file extensions.
The function I have written is below. I am not sure if this follows best practices. Also, the hack using the brace expansion with the .\?
is bad. I really appreciate any feedback that you have.
findInFile() {
# Function to be placed in bash profile
# Allows user to specify what contents of a file they are looking for
# in what file types and in what NOT file types
# Iterate over the number of scripts arugments
# TODO: How can I create documentation for this function outside of code comments?
while [[ "$#" -gt 0 ]]
do
case $1 in
-ft|--fileTypes)
local fileTypes=$2
;;
-et|--excludeTypes)
local excludeTypes=$2
;;
*) # Catch the case where user does not specify
# these arguments
local searchTerm=$1
;;
esac
shift
done
echo "fileTypes: $fileTypes"
echo "excludeTypes: $excludeTypes"
echo "searchTerm: $searchTerm"
# TODO: Should probably clean up with case statement
# TODO: I am using this \? in the include and exclude as a hack
# to catch the case where only one file type is provided.
if [ -n "$fileTypes" ] && [ -n "$excludeTypes" ]
then
#searchString="grep -r --include=\*{$fileTypes} --exclude=\*{$excludeTypes} "$searchTerm" ."
searchString="grep -r --include=\*{$fileTypes,.\?} --exclude=\*{$excludeTypes,.\?} "$searchTerm" ."
elif [ -n "$fileTypes" ]
then
#searchString="grep -r --include=\*{$fileTypes} "$searchTerm" ."
searchString="grep -r --include=\*{$fileTypes,.\?} "$searchTerm" ."
elif [ -n "$excludeTypes" ]
then
#searchString="grep -r --exclude=\*{$excludeTypes} "$searchTerm" ."
searchString="grep -r --exclude=\*{$excludeTypes,.\?} "$searchTerm" ."
else
searchString="grep -r "$searchTerm" ."
fi
#searchString="grep -r --include=\*{$fileTypes} "$searchTerm" ."
echo "searchString: $searchString"
eval $searchString
# TODO: Allow the user to type a number to then programmatically jump to that
# file in the text editor of their choice
}
grep -r --include=.C "test string"
becomesack --cc "test string"
beyondgrep.com/feature-comparison beyondgrep.com/why-ack \$\endgroup\$ag
, the silver searcher -- very fast, automatically recursive, PCRE regexes. \$\endgroup\$