How to extract log info based on last month?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract log info based on last month?
# 1  
Old 08-13-2010
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:
Code:
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:
Code:
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.
# 2  
Old 08-13-2010
Quote:
Originally Posted by superHonda123
...
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.
...
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:

Code:
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:

Code:
date --date='2 months ago'

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

Code:
$
$ 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" :

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

tyler_durden
# 3  
Old 08-13-2010
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. Smilie

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.

Code:
# 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:
Code:
SHELL=/sbin/sh

Please kindly advice.

Thanks.

Cheers.

Last edited by Scott; 08-14-2010 at 06:47 AM.. Reason: Code tags
# 4  
Old 08-13-2010
Quote:
Originally Posted by superHonda123
...
When I tried your method, there are some errors as shown below. I'm unable to use your suggested option of date function.
...
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.

Last edited by durden_tyler; 08-13-2010 at 02:37 PM..
# 5  
Old 08-14-2010
Hi Gurus,

Is there any other ways to solve this problem?

Please kindly advice.

Thanks.


Cheers.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

2. Shell Programming and Scripting

Moving log files based on month - help with ksh shell script

Hello, I'm trying to move the log files from the parent directory to respective monthly folder and I would be running this script on a weekly basis through cron. I'm new to this scripting and here is what i could come up and it runs without really doing anything. I even tried placing echo... (2 Replies)
Discussion started by: jusblues
2 Replies

3. Shell Programming and Scripting

Extract portion of log info based on specific word

Hi Gurus, I'm using HP-UX B.11.23 operating system. I've been trying to extract a specific wording for example: "A tool used by tp produced warnings" from my below log data, but could not find a way to solve it. My intention is, if the log contain the word: "A tool used by tp produced... (9 Replies)
Discussion started by: superHonda123
9 Replies

4. Shell Programming and Scripting

How to extract log data based on current date and month ?

Hi Gurus, I'm using HP-UX B.11.23 operating system. I've been trying to extract this log info based on the current date and month, but was having some issues as the date column which on the 4th column has a comma and the 5th column has a dot tied to it. Here is the output from my shut... (5 Replies)
Discussion started by: superHonda123
5 Replies

5. Shell Programming and Scripting

How to extract log data based on date

Hi Gurus, I've been having some problem in extracting the log data based on the current date and month. As shown in the sample data below, how to extract the log info for Aug 11? Sample data: root pts/ta userpc Wed Aug 11 09:46 - 20:21 (10:35) root pts/ta userpc... (13 Replies)
Discussion started by: superHonda123
13 Replies

6. Shell Programming and Scripting

Extract info from log file and compute using time date stamp

Looking for a shell script or a simple perl script . I am new to scripting and not very good at it . I have 2 directories . One of them holds a text file with list of files in it and the second one is a daily log which shows the file completion time. I need to co-relate both and make a report. ... (0 Replies)
Discussion started by: breez_drew
0 Replies

7. Shell Programming and Scripting

How can i extract month number ?

I wanted to extract the month number from unix. unixdev(invent) /home/odsapp/apps/CTSI ==> date Tue Nov 11 14:42:27 EST 2008 From date command, i can get day(11), year(2008). But i need month number.. I am receiving files as test11102008.txt My need to identify when this file is... (1 Reply)
Discussion started by: govindts
1 Replies

8. IP Networking

How to extract NIC and other info ,given Ip

Hi all, I am working on a networking project that requires me to find out the NIC on that particular machine and many more things. Now Given the IP and the subnet. I would like to know how we can extract such informations? I am talking in exclusivity to Solaris boxes!! The required... (6 Replies)
Discussion started by: wrapster
6 Replies

9. AIX

Extract info

Anyone have a better idea to automate extraction of info like ... "uname" "ifconfig" "ps efl" "netstat -ao" etc. from several hundred aix, solaris, red hat boxes? without logging into each box and manually performing these tasks and dumping them to individual files? thanks for any input (1 Reply)
Discussion started by: chm0dvii
1 Replies

10. AIX

need to extract info from log files

hi guys i need to extract information from log files generated by an application. log file has the following lines for each process.. ---------------------------------------------- Fri Aug 03 12:06:43 WST 2007 INFO: Running project PROJECT1 Fri Aug 03 12:06:43 WST 2007 INFO: Source Files... (7 Replies)
Discussion started by: kirantalla
7 Replies
Login or Register to Ask a Question