[Solved] Fetching logs of last few days from logfile


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Fetching logs of last few days from logfile
# 15  
Old 08-30-2012
Yes this can be done..
Good option, but it giving each time some output... and not sure at what valuue of 'p' there will be an existing date in log file.

So after each execution of command its giving some output.
# 16  
Old 08-30-2012
CAN YOU GIVE SOME SAMPLE DATA and you required output so that i can come to some conclusion
# 17  
Old 08-30-2012
Yes Raj, this can be done but problem is that we are not sure that at what value of 'p' there will be an existing date in logfile.
So for each value of 'p' command will get executed and hence resulting in some unexpected response.

---------- Post updated at 02:27 AM ---------- Previous update was at 02:21 AM ----------

sample data
:

2012 Aug 01 : sdfjsdfkljsdlfgjlsdfjgldjflgjsdfgasdfg
2012 Aug 05 10:12:dfkjsdlfksdfksdfks;dkf;sdkf;ksdfjsdf
2012 Aug 05 10:13: kfgsdklfjlsedjkflsdjkfljsdfgsda
2012 Aug 30 10:11: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf

So if i am asking data of last 30 days it should give whole data from 2012 Aug 01 to 2012 Aug 30.
if last 25 days then:
2012 Aug 05 10:12:dfkjsdlfksdfksdfks;dkf;sdkf;ksdfjsdf
2012 Aug 05 10:13: kfgsdklfjlsedjkflsdjkfljsdfgsda
2012 Aug 30 10:11: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf

But if i running it for last 29 days it should give
2012 Aug 05 10:12:dfkjsdlfksdfksdfks;dkf;sdkf;ksdfjsdf
2012 Aug 05 10:13: kfgsdklfjlsedjkflsdjkfljsdfgsda
2012 Aug 30 10:11: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf

but not of 2012 Aug 01
# 18  
Old 08-30-2012
Try this...

Code:
$cat file
2012 Aug 01 : sdfjsdfkljsdlfgjlsdfjgldjflgjsdfgasdfg
2012 Aug 05 10:12:dfkjsdlfksdfksdfks;dkf;sdkf;ksdfjsdf
2012 Aug 05 10:13: kfgsdklfjlsedjkflsdjkfljsdfgsda
2012 Aug 30 10:11: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf
2012 Aug 31 10:12: sdhfsdkfsdjflsjdfjsdf

#I don't know how <= is not working for the end date so add one more date to your end date...

$ awk -F ":" '{ if ($1 >= "2012 Aug 01" && $1 <= "2012 Aug 31" ) print }' file
2012 Aug 01 : sdfjsdfkljsdlfgjlsdfjgldjflgjsdfgasdfg
2012 Aug 05 10:12:dfkjsdlfksdfksdfks;dkf;sdkf;ksdfjsdf
2012 Aug 05 10:13: kfgsdklfjlsedjkflsdjkfljsdfgsda
2012 Aug 30 10:11: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf

$ awk -F ":" '{ if ($1 >= "2012 Aug 01" && $1 <= "2012 Sep 1" ) print }' file
2012 Aug 01 : sdfjsdfkljsdlfgjlsdfjgldjflgjsdfgasdfg
2012 Aug 05 10:12:dfkjsdlfksdfksdfks;dkf;sdkf;ksdfjsdf
2012 Aug 05 10:13: kfgsdklfjlsedjkflsdjkfljsdfgsda
2012 Aug 30 10:11: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf
2012 Aug 31 10:12: sdhfsdkfsdjflsjdfjsdf

# 19  
Old 08-30-2012
and the code to extract last nth day's date is
Code:
START=`TZ="GMT+$((24*N))" date +"%Y %b %d"`

---------- Post updated at 02:50 AM ---------- Previous update was at 02:44 AM ----------

Yes Pamu, i can do this too, good option but in case if date format changed.
will this work?
Specially in awk -F ":" the ":" symbol. beacuse in another file this can be "-" or ";"

Last edited by Franklin52; 08-30-2012 at 08:10 AM.. Reason: Please use code tags for data and code samples
# 20  
Old 08-30-2012
Quote:
Originally Posted by KDMishra
and the code to extract last nth day's date is
START=`TZ="GMT+$((24*N))" date +"%Y %b %d"`

---------- Post updated at 02:50 AM ---------- Previous update was at 02:44 AM ----------

Yes Pamu, i can do this too, good option but in case if date format changed.
will this work?
Specially in awk -F ":" the ":" symbol. beacuse in another file this can be "-" or ";"
-F ":" this used as field separator. If date format changed by that means field separator changed then we have to implement that to the code. But this is not major change you can do it easily.

for START variable..

You can use below code to assign date variable to one day ahead of today's date...

Code:
START=$(date --date="tomorrow" +"%Y %b %d")

# 21  
Old 08-30-2012
whether the log will be of one month only or will be of another month also
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fetching timestamps from the logs.

Dear Experts, I need some help to get the time stamps from the Unix logs. Basically I am want to analyze which step is taking more time. Please give me some inputs. Thanks a lot for your help. I have got different codes for each for phase as we can see in the logs 00002 – UPDT Starting... (5 Replies)
Discussion started by: srikanth38
5 Replies

2. Shell Programming and Scripting

Deleting 3 days old logs from a directory

As i am working in unix environment so i have an logs that is created by my application at the following location that is /opt/app/glac/current/servers/ops/logs inside the logs directory there are different kinds of logs(that is the file having extension as .log ) have been created... (1 Reply)
Discussion started by: 2015nks
1 Replies

3. Shell Programming and Scripting

Logs from logfile

Hi Team, Have to write a shell script to pick only 1 hr logs from the generated logfile and send it to other logfile. Thanks & Regards, Indu (3 Replies)
Discussion started by: indira_s
3 Replies

4. Shell Programming and Scripting

[Solved] Remove file older than 90 days

I have crontab job a tar file to a directory ( tar -cvf /tmp/backup/or.`date +%m%d%y`. /ora/db/* ) , it will do it every day . Now I don't want to keep too much files , I just want to keep the file for 90 days , can advise if I want to remove the backup file which are elder than 90 days , can... (1 Reply)
Discussion started by: ust3
1 Replies

5. Shell Programming and Scripting

Delete logs older than 60 days

i am using HP-UX OS...... delete logs older than 60 days find -mtime +60 | grep -i '.*log' | xargs rm -mtime is nt available in HP-UX, pls tell me other option ? (2 Replies)
Discussion started by: only4satish
2 Replies

6. Shell Programming and Scripting

To delete logs older than 30 days

I want to write a shell script that deletes all log files in a directory that are older than 30 days except for 3 files: I am using the following command: find /tmp/logs -name "*.log" -mtime +30 -exec rm -f {} \;But this command deletes all the log files. How can i modify this script that... (5 Replies)
Discussion started by: mmunir
5 Replies

7. Shell Programming and Scripting

How to remove the logs more than 5 days old

All, How to remove the logs that are more than 5 days old from a particular folder. Help will be highly appreciated. Regards Oracle User (2 Replies)
Discussion started by: Oracle_User
2 Replies

8. Shell Programming and Scripting

Logs access in windows fetching the data from a unix server

How I can get the logs that are getting stored in specific location in unix server through an Apache web server installed in unix server? Requirement is to access the logs through the URL in windows browser without any access. (1 Reply)
Discussion started by: alvida
1 Replies

9. UNIX for Dummies Questions & Answers

Delete last 10 days logs

Hi Can u please tell me how to delete last 10 days logs .. (9 Replies)
Discussion started by: pb18798
9 Replies

10. UNIX for Advanced & Expert Users

how to archive logs older than 5 days & then delete them?

My code is tar -cvf logs.tar `find /usr/openv/logs/512*.log -mtime +2` && find *.log* -mtime +2 -exec rm {} \; this gives me output as: tar: Missing filenames:confused: (1 Reply)
Discussion started by: timus1980
1 Replies
Login or Register to Ask a Question