471,122 Members | 1,153 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

.sh programming help

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
0 2884

Post your reply

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

Similar topics

4 posts views Thread by Mark | last post: by
2 posts views Thread by Mike Maxwell | last post: by
12 posts views Thread by G. | last post: by
26 posts views Thread by Kanthi Kiran Narisetti | last post: by
42 posts views Thread by Kevin Spencer | last post: by
25 posts views Thread by Dave | last post: by
20 posts views Thread by Mike | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.