Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
The problem statement, all variables and given/known data:
My goal to find how many requests in 14 days from weblog server. I know to cat a weblog file to wc -l to find the total of all requests have send to server. From there on, I can use a loop of 14 days to go through each line and do comparison with Date String until the string are different I would increment the date until it reach 14th date. I was thinking to seq 14 and xargs commands to go through 14 days cycle.
I would like to know how to do a string compare between date?
I'm deliberately posting the following command for various reasons:
To prove that OP's idea might work under certain/ideal circumstances (non-strict accuracy eg. (roughly) to a day, it's the second half of the month etc.)
This works and it's counting lines for timestamps from 24/Nov/2014 to 10/Nov/2014 (last 14 days).
Now imagine it's Nov 5 ...
Following hints should help you to do it right and to gain an accuracy to a second:
Get the last line of access.log (newest entry)
Cut the date-time-timezone stamp ( 10/Jun/2013:07:43:40 -0700 )
Reformat above string to something you can feed to GNU date, e.g. 2013-06-10 07:43:40 OR 2013/06/10 07:43:40 OR 20130610 07:43:40 OR whatever
Using GNU date, convert the reformatted string to seconds since 1970-01-01
4.1 Subtract 60�60�24�14 seconds (14 days) from the result and put the result in a variable, eg. twoweeksago
In a while loop you then parse the access.log file line by line
6.1. You repeat the steps 2, 3 and 4 here
6.2. Pseudo code: if seconds greater than/equal to twoweeksago then increment counter
How do I remove the square bracket from most left? I have tried to use grep but
Unsuccessful to get rid of the bracket. After removed the bracket I think I can use
date "14 days ago" to set a stop point where I can count for all requests between those
date. I hope I got the right approach from here.
It will retrieve only the very last line and apply the cut command to it.
You could simply add | cut -c2- to the above command. It will cut from the second character on until the end of the line, thus removing the square bracket.
In a loop, the above really should be done by making use of bash's built-ins for better performance.
We are not here to do your homework for you. Leaving section 3 in the homework template blank isn't a good starting point.
If you put in some effort and show us what you have tried, we can help you hone your solution to one that will get the job done. If you aren't willing to show us that you are making an effort, there isn't much more that we will be willing to do for you.
All that you have shown us is that you can use cut with space as a delimiter to select part of an input line. Could you use cut with a different delimiter to get rid of the [ ?
What tools are you allowed to use for this assignment? You have shown us cut and we assume that you can use some shell (but we don't know which shell). You have suggestions to use GNU date and awk . Are these available for this assignment? Can you use a 1993 or later version of ksh (which can also perform simple calculations to get the date 10 days ago)?
Are you looking for a date 14 days ago to the second? Or, are you looking for a date starting at midnight almost 14 days ago? Whether or not you can use the date utility depends on what operating system and shell you're using?
I have thought about how to approach how to write this program but did not knowing how the log system work. There fore, I ask some questions here may offend the integrity of the forum.
First I thought if I knew how to manipulate the date string then I can control the log count of every 14 days. So I wonder how to grep things and strip things off. I had problem with bracket attach to date format such as
[15/Nov/��.
From the last lecture we have learned using Tr command I found it is useful to apply on how to remove the bracket.
I realized the easiest way to get the total requests of every 14 days
is find the line number of every break point of 14 days�� 0, 14, 24, 42 etc.
Pseudo:
Find the last line of the most current time in log file
Find the line number of request 14 days ago ("+%d/%b/%Y:%H:%M")
Then subtract the different to get the total number request of 14 days ago.