logfind
Platform: Shell
Published Aug 04, 2012
Updated Aug 05, 2012
First post!
I've been working on this for a little bit now, figured I would share as I have found it very useful so far.
Usage: ./logfind [options]
-c Count search term and display amount found
-s Filter search terms for one file only
-v Print version and exit
-h Print this and exit
-i Use this to make the search case-insensitive
-m [arg] Limit the amount of search results to argument
-o [arg] Outputs to specified file
Examples:
./logfind -sic
This would search a single file for the given term, ignoring
the case, and display the number of results
./logfind -s
This would search a single file for the given term and
display the results
./logfind -is -m 20 -o /var/log/logfind-output
This would search a single file, ignoring the case,
with a limit of 20 results, outputting them to a file
ziddy@bt:~$ cat logfind | grep -c exit
19
ಠ_ಠ
#!/bin/bash
#
#
# Name: logfind
# Author: Ziddy (Jeff)
# Version: v1.0.4
# Purpose: Easy filtering of data from one or multiple logfiles
#
# Created: Aug 4th, 2012
# Last Rev: Aug 4th, 2012
###############################################################################
clear
function callHelp {
echo -e "Usage: $0 [options]\n"
echo -e "\t-c\t\tCount search term and display amount found"
echo -e "\t-s\t\tFilter search terms for one file only"
echo -e "\t-v\t\tPrint version and exit"
echo -e "\t-h\t\tPrint this and exit"
echo -e "\t-i\t\tUse this to make the search case-insensitive\n"
echo -e "\t-m [arg]\tLimit the amount of search results to argument\n"
echo -e "\t-o [arg]\tOutputs to specified file"
echo -e "\n\tExamples:"
echo -e "\t$0 -sic"
echo -e "\t\tThis would search a single file for the given term, ignoring \n\tthe case, and display the number of results\n"
echo -e "\t$0 -s"
echo -e "\t\tThis would search a single file for the given term and \n\tdisplay the results\n"
echo -e "\t$0 -is -m 20 -o /var/log/logfind-output\n"
echo -e "\t\tThis would search a single file, ignoring the case, \n\twith a limit of 20 results, outputting them to a file\n"
exit
}
while getopts cvshio:m: opt
do
case "$opt" in
c) COUNT=true;;
v) echo "logfind v1.0.4 coded by Ziddy"
exit;;
s) SINGLE=true;;
h) callHelp;;
i) CASE="-i";;
m) LIMIT="-m"
VALUE=$OPTARG;;
o) OUTPUT=true
DEST=$OPTARG;;
\?)
callHelp;;
esac
done
echo "Log searcher v1.0.4 written by Ziddy"
echo -n -e "Please enter a search term - ex: hello\nInput: "
read sTerm
if [ $SINGLE ] && [ $COUNT ]; then
echo -n -e "What file do you want to search and count in?\nInput: "
read sFile
if [ $OUTPUT ]; then
cat $sFile 2> /dev/null | grep -c $CASE $LIMIT $VALUE "$sTerm" | tee -a $DEST
exit
else
cat $sFile 2> /dev/null | grep -c $CASE $LIMIT $VALUE "$sTerm"
exit
fi
fi
if [ $SINGLE ]; then
echo -n -e "What file do you want to search in? - ex: /var/log/auth.log\nInput: "
read sFile
clear
echo "Working..."
if [ $OUTPUT ]; then
cat $sFile 2> /dev/null | grep -a $CASE $LIMIT $VALUE "$sTerm" | tee -a $DEST
exit
else
cat $sFile 2> /dev/null | grep -a $CASE $LIMIT $VALUE "$sTerm"
exit
fi
exit
else
echo -n -e "Where are the log files located? - ex: /var/log/\nInput: "
read sLocation
fi
echo -n -e "What is the log suffix? - ex: log\nNote: This can be blank\nInput: "
read sSuffix
if [ $COUNT ]; then
clear
echo "Working..."
echo -n "Results found: "
if [ ${#sSuffix} -lt "1" ]; then
if [ $OUTPUT ]; then
cat $sLocation* 2> /dev/null | grep -c $CASE $LIMIT $VALUE "$sTerm" | tee -a $DEST
exit
else
cat $sLocation* 2> /dev/null | grep -c $CASE $LIMIT $VALUE "$sTerm"
exit
fi
exit
else
if [ $OUTPUT ]; then
cat $sLocation*.$sSuffix 2> /dev/null | grep -c $CASE $LIMIT $VALUE "$sTerm" | tee -a $DEST
exit
else
cat $sLocation*.$sSuffix 2> /dev/null | grep -c $CASE $LIMIT $VALUE "$sTerm"
exit
fi
fi
fi
if [ ${#sSuffix} -lt "1" ]; then
clear
echo "Working..."
if [ $OUTPUT ]; then
cat $sLocation* 2> /dev/null | grep -a $CASE $LIMIT $VALUE "$sTerm" | tee -a $DEST
exit
else
cat $sLocation* 2> /dev/null | grep -a $CASE $LIMIT $VALUE "$sTerm"
exit
fi
else
clear
echo "Working..."
if [ $OUTPUT ]; then
cat $sLocation*.$sSuffix 2> /dev/null | grep -a $LIMIT $VALUE $CASE "$sTerm" | tee -a $DEST
exit
else
cat $sLocation*.$sSuffix 2> /dev/null | grep -a $LIMIT $VALUE $CASE "$sTerm"
exit
fi
fi
exit