So what we have is a .txt file containing lines like the following:
66.196.90.230 - - [01/Mar/2004:01:28:59 +1000] "GET /robots.txt HTTP/1.0" 404 282
My current problem is I cannot figure out how to break the text into substrings based on field separators of "/", and only to use the month(Mar) or year(2004). Also need a counter which is updated according to the substrings found in the input data.
This is what I have so far, and it doesn't function like I want:
# process all arguments (i.e. loop while $1 is present)I think its the two changes of the IFS, but I cant find anything in my notes on how else to do this. Thanks for any help.
while [ -n "$1" ] ; do
# echo "Arg is $1"
case $1 in
-h*|-H*) echo "help msg1" ; echo "help msg2" ;
shift ;;
[12][0-9][0-9][0-9]) the_year=$1 ;
shift ;;
Jan*|jan*|JAN*) the_month="Jan" ; shift ;;
Feb*|feb*|FEB*) the_month="Feb" ; shift ;;
Mar*|mar*|MAR*) the_month="Mar" ; shift ;;
Apr*|apr*|APR*) the_month="Apr" ; shift ;;
May*|may*|MAY*) the_month="May" ; shift ;;
Jun*|jun*|JUN*) the_month="Jun" ; shift ;;
Jul*|jul*|JUL*) the_month="Jul" ; shift ;;
Aug*|aug*|AUG*) the_month="Aug" ; shift ;;
Sep*|sep*|SEP*) the_month="Sep" ; shift ;;
Oct*|oct*|OCT*) the_month="Oct" ; shift ;;
Nov*|nov*|NOV*) the_month="Nov" ; shift ;;
Dec*|dec*|DEC*) the_month="Dec" ; shift ;;
*) echo "? unrecognised input: $1" ;
shift ;;
esac
done
echo "Got month: $the_month"
echo "Got year: $the_year"
oldIFS="$IFS"
count=0
IFS="/"; while read file
do
set $file
IFS=":"
set $1
if [ "$3" = "$the_year" ]
then
echo $file
count=`expr $count + 1`
else
if [ "$2" = "$the_month" ]
then
echo $file
count=`expr $count + 1`
fi
fi
done < my_sample_log2.txt
echo $count
IFS=$oldIFS