How to extract log info based on last month?

Hi Gurus

I'm using HPUX B.11.23 ia64, the below sample log data was extracted from HPUX commnad: last -R.

Sample data:

root     pts/ta       userpc Wed Aug 11 09:46 - 20:21  (10:35)
root     pts/ta       userpc Wed Aug 11 09:44 - 20:10  (10:34)
root     pts/ta       userpc Wed Aug 11 09:40 - 20:05  (10:32)
root     pts/tb       userpc Tue Aug   9 11:23 - 11:23  (00:00)
root     pts/tb       userpc Tue Aug   9 11:22 - 11:20  (00:00)
root     pts/ta       userpc Tue Aug   2 10:46 - 19:19  (08:33)
root     pts/ta       userpc Tue Aug   2 10:40 - 19:15  (08:33)
root     pts/ta       userpc Tue Jul   31 10:41 - 10:45  (00:04)
root     pts/ta       userpc Tue Jul   10 09:40 - 10:43  (00:04)
root     pts/ta       userpc Tue Jul    1 05:40 - 10:43  (00:04)

I'm using a cronjob to execute my script on monthly basis, for example: the script will be executed on every 1st date of the month.

The issue is, how to extract the log data based on last month?

For example:
Let's say the month of august 2010 is already over, now I wanted to extract all log info from July.

Expected output:

root     pts/ta       userpc Tue Jul   31 10:41 - 10:45  (00:04)
root     pts/ta       userpc Tue Jul   10 09:40 - 10:43  (00:04)
root     pts/ta       userpc Tue Jul    1 05:40 - 10:43  (00:04)

Please kindly advice.

Thanks a lot.

Cheers.

If August 2010 is already over, then we are either in September 2010 or later. In that case you'll have to jump at least 2 months prior.

But your first question is about one month prior. In any case, if you have GNU date in your system, then:

date --date='1 month ago'

returns the date one month ago as compared to today's date and time.
And ditto for 2 months prior:

date --date='2 months ago'

And the format string "%b" returns just the month in 3-character code, like so -

$
$ date --date='1 month ago' '+%b'
Jul
$

It should be easy, therefore, to grep for occurrences of prior month in the output of "last -R" :

last -R | grep $(date --date='1 month ago' '+%b')

tyler_durden

Hi Gurus,

Yes, when I read my posting again, I realised that I've made some mistakes in which may cause some confusion. Sorry about that. :frowning:

Here are the corrections.

For example:
Let's say the month of august 2010 is already over, so that means we now in september 2010. Based on my cronjob task on 1st of September, how to extract the log info of the previous month, in this case, August?

My script will always extract the the log info from the previous month.

Please kindly advice.

Thanks a lot.

Cheers.

---------- Post updated at 11:02 AM ---------- Previous update was at 10:56 AM ----------

Hi tyler_durden,

Thanks for your response.

When I tried your method, there are some errors as shown below. I'm unable to use your suggested option of date function.

# date --date='1 month ago' '+%b'
date: illegal option -- -
Usage: date [-u] [+format]
       date [-u] [mmddhhmm[[cc]yy]]
       date [-a [-]sss.fff]

# last -R | grep $(date --date='1 month ago' '+%b')
date: illegal option -- -
Usage: date [-u] [+format]
       date [-u] [mmddhhmm[[cc]yy]]
       date [-a [-]sss.fff]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvwx] -e pattern_list...
        [-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvwx] [-e pattern_list...]
        -f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvwx] pattern [file...]

When I checked my environment, it shows the following:

SHELL=/sbin/sh

Please kindly advice.

Thanks.

Cheers.

The solution will work only with GNU date, as mentioned in my earlier post. Do you have GNU date in your system ?

tyler_durden

Ok, just noticed you have HPUX. Search this forum for Perderabo's suite of shell functions that perform date arithmetic. Alternatively, you may want to use a scripting language to do the same.

Hi Gurus,

Is there any other ways to solve this problem?

Please kindly advice.

Thanks.

Cheers.