I'm only new to shell programming and have been given a task to do a program in .sh, however I've come to a point where I'm not sure what to do. This is my code so far:
# process all arguments (i.e. loop while $1 is present)
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
What my problem is where the IFS changes happen. I need to break the text into substrings based on field separators of "/" - only interested in the month and year patterns. Also need a counter/s which are updated according to the substrings found in the input data.
My program isn't working, and I have a feeling it is to do with two IFS changes, but I don't know how else to break the string up to just get the Month or year. The lines in the file look something like:
66.196.90.230 - - [01/Mar/2006:01:28:59 +1000] "GET /file.txt HTTP/1.0" 404 282
Thanks.