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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract log data based on current date and month ?
# 1  
Old 09-13-2010
Question 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 down log info.

Code:
cat /etc/shutdownlog | tail -10

13:36  Sun Dec 8, 2009.  Reboot:  (by root)
13:56  Sun Dec 10, 2009.  Halt:  (by root)
15:12  Sun Dec 27, 2009.  Reboot:  (by root)
13:46  Sun Apr 1, 2010.  Halt:
14:43  Sun Apr 8, 2010.  Halt:
17:28  Sun Apr 18, 2010.  Reboot:  (by root)
18:15  Thu Jul  1, 2010.  Reboot:  (by  root)
12:01  Mon Aug 30, 2010.  Reboot:  (by root)

Previously, I got some help some Gurus in this forum to use this type of AWK as a function module. Please see below.

AWKDATEMTH() {
awk -v v1=`date +'%h'` -v v2=`date +'%d'` -v v3=`awk '{print $4}' | tr -d ','` '$3==v1 && v3==v2'}

However, I've been trying to fix it to cater for my new log data, but was to no luck. Guess my scripting level is limited.

Appreciate for any of your help and advice.

Thank you in advance.

Cheers.
# 2  
Old 09-13-2010
Not quite sure what "the current date and month" means. If you mean today's date we can output the date complete with the comma.
Note the use of "%e" (day with leading space) because I think this is the format used in /etc/shutdownlog though the sample log posted contains inconsistent spacing for single digit dates.


Code:
#!/bin/sh
TODAY=`date +'%a %b %e, %Y'`
grep "${TODAY}" /etc/shutdownlog

# 3  
Old 09-14-2010
Hi methyl,

Thanks for your response.

The current date and month is referring to today's date and month as this script will be scheduled to run daily and monthly.

I've tried your method and it seems to be OK. Please see below.

Code:
# TODAY=`date +'%a %b %e, %Y'`

# echo $TODAY
Tue Sep 14, 2010

However, as shown below, some of the dates are not always in 2 digits, then your method might not work when the date is smaller than 10, single digit.

Code:
Sample log data:
----------------
13:36  Sun Dec 8, 2009.  Reboot:  (by root)
13:56  Sun Dec 10, 2009.  Halt:  (by root)
15:12  Sun Dec 27, 2009.  Reboot:  (by root)
13:46  Sun Apr 1, 2010.  Halt:
14:43  Sun Apr 8, 2010.  Halt:
17:28  Sun Apr 18, 2010.  Reboot:  (by root)
18:15  Thu Jul  1, 2010.  Reboot:  (by  root)
12:01  Mon Aug 30, 2010.  Reboot:  (by root)

Please kindly advice.

Thanks.

Cheers.

Last edited by superHonda123; 09-14-2010 at 02:58 AM.. Reason: add more info
# 4  
Old 09-14-2010
hi,

Try this,

Code:
yr=`date '+%Y'` ; dt=`date '+%d'`;mon=`date '+%b'`
awk -v v1=$dt -v v2=$mon -v v3=$yr '{v1=v1+0;if(v2==$3 && v3==substr($5,1,length($5)-1) && v1==substr($4,1,(length($4) -1))) {print $0}}' inputfile

This User Gave Thanks to pravin27 For This Post:
# 5  
Old 09-14-2010
Hi pravin27,

Thanks for your response.

I've tried your method and it seems to be working. GREAT.

I will check and monitor it.

I've been looking for this type of script.

Appreciate for your help.

Thanks.

Cheers.
# 6  
Old 09-14-2010
The format of the messages in /etc/shutdownlog does seem to vary.

Anybody got a sample "Reboot after panic" message from HP-UX B.11.23 ? On earlier versions this message has leading zeros on the day field an no comma.

Note: If you want to know whether the system rebooted, the "who -b" command is a lot more reliable. There is no entry in /etc/shutdownlog after catastrophes such as a power failure.

Btw. I think that the O/P references to the "Date" actually mean the "Day" component of a calendar date.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to get first & last day of a month from current date?

Hi, I need the first & last day of a month from any given date. For better understanding, if i need to back-fill data for date 07/20/2019 i.e July 20 2019, i need the first & last day has 07/01/2019 - 07/31/2019. FYI: I'm using GIT BASH terminal. sample code: export DT=$(date --date='6 days... (2 Replies)
Discussion started by: Rocky975583
2 Replies

2. Homework & Coursework Questions

How to minus 2 month from current date?

I am running a script in ksh to get the 2 months back date from system date.The below code is giving correct date output from putty command prompt.But while running the script is .ksh file it is giving the error below.Please suggest. ; d=a; y=a m-=num while(m < 1) {m+=12; y--}... (1 Reply)
Discussion started by: hini
1 Replies

3. AIX

AIX - Get next month from current date

As said in object, how can i obtain that? In linux i use date -d "1 month" +"%m%Y". Thanks i advance. (8 Replies)
Discussion started by: fabfisc
8 Replies

4. UNIX for Dummies Questions & Answers

Extract date ranged data from log file

Hi, I am trying to extract lines of data within a log file on a Redhat 5 Linux system. eg I need all the lines with a particular username over the last 3 minutes. the log file may read like this, and I want a way to search all the lines extracting all the relevant lines over the last 3... (2 Replies)
Discussion started by: mantis100
2 Replies

5. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

6. Shell Programming and Scripting

current date - one month in AIX

Hi, i unable to get the last month date in AIX. current date - one month Based on the forums tried but did not find the relevent information. Any help grealy appriciated. Thanks Suri. (3 Replies)
Discussion started by: onesuri
3 Replies

7. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: superHonda123
4 Replies

8. 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

9. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

10. Shell Programming and Scripting

How to find the create time of a file if current date is in next month

Hi All, I want to find the time diffrence between currnt time and "abc.txt" file create time. I have solve that but if the abc.txt file created last month then is there any process to find the difftent? Exp: Create time of abc.txt is "Apr 14 06:48" and currect date is "May 17 23:47".... (1 Reply)
Discussion started by: priyankak
1 Replies
Login or Register to Ask a Question