I am trying to create a script which will query a log file (or files) for a particular value and using the values given print out not just the line that it is on but all the lines around it as long as they are not blank.
Essentially I want to run it like this:
logSearch.sh -f FilenameToSearch.log -s SearchTerm -b "20130323 12:00:00" e- "20130323 14:21:00"
FilenameToSearch.log contains the following:
03-10-2013 12:11:30
JunkData
JunkData
03-23-2013 12:00:00
JunkStill
Since
ValueLooking
ForIs
NotHere
03-23-2013 12:10:00
NotJunk
SearchTerm
ValueHere
NeedTo
GetAll
Yay
03-23-2013 12:10:30
BackToJunk
Blah
03-23-2013 12:11:00
SearchTerm
MorePrint
03-23-2013 15:10:00
SearchTerm
ButAfterGiven
Time
So
Junk
It will search the log file and find anything that matches the first search term in between the date-time values given (they are optional).
So it would return the following and pipe it to a new file:
03-23-2013 12:10:00
NotJunk
SearchTerm
ValueHere
NeedTo
GetAll
Yay
03-23-2013 12:11:00
SearchTerm
MorePrint
I have the basic code here I just havent gotten to actually processing the data yet so anything that you all could help with would be VERY APPRECIATED! I will keep working on this throughout the week and flesh it out more as the days progress but any ideas you have (since yall are likely better at writing these scripts) would be useful
Code
#!/bin/bash -eu
sytax="Proper invocation is logSearch.sh -s search-term -f filename-directory [b - datetimevalue1] [e - datetimevalue2]";
necVal="Error. -s and -f need to be specified";
usage () { echo $sytax; exit 1;}
error () { echo $necVal; usage; exit 2;}
options='s:f:b:e:x'
while getopts $options option
do
case $option in
s)
searchFor=$OPTARG
;;
f)
inFilesLike=$OPTARG
;;
b)
betweenThis=$OPTARG
;;
e)
andThis=$OPTARG
;;
*)
usage;
;;
esac
done
shift $(($OPTIND - 1))
if [ -z ${searchFor:-} ] || [ -z ${inFilesLike:-} ]
then
error;
else
echo "Search for : " $searchFor;
echo "in files like: " $inFilesLike;
fi
if [ ! -z ${betweenThis:-} ]
then
echo "Starting search at first occurance of: " $betweenThis;
else
echo "No value to start search from. Beginning from the start of file(s).";
fi
if [ ! -z ${andThis:-} ]
then
echo "Ending search at first occurance of: " $andThis;
else
echo "No value to end search at. Will search to the end of file(s).";
fi
#BEGIN CODE TO SEARCH THROUGH $INFILESLIKE FILES FOR PARTICULAR VALUES