Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DanLaufer/9c8746a4756a8c18227bc30062e02778 to your computer and use it in GitHub Desktop.
Save DanLaufer/9c8746a4756a8c18227bc30062e02778 to your computer and use it in GitHub Desktop.
Bash - Function to check for an odd number of occurances of a string in a file
# $1 is the name of the element to search
# $2 is the file to search
function checkodd(){
if [ $# -ne 2 ]; then
echo "Please use the following format: checkodd <string to check> <location>"
else
for f in "$2"/* "$2"/**/* ; do
if [ -f "$f" ]; then
stringcount=$(grep -o $1 $f | wc -l)
if [ $((stringcount % 2)) -eq 1 ]; then
echo $stringcount' '$1': '$f
fi
fi
done;
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment