By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,271 Members | 1,231 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,271 IT Pros & Developers. It's quick & easy.

.sh programming help

P: 4
I'm only new to learning shell programming and we have this current task to create a .sh program (basic shell only) that can output a web access statistics report of accesses for each month in a year or total number of accesses in a particular month in a year selected by the user.

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)
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
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.
Apr 6 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.