Script to read last 30mins logs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to read last 30mins logs
# 1  
Old 09-28-2016
Script to read last 30mins logs

Hi All,

I want to read the log file for last 30mins logs with time stamps.

Am using below command but, it is not working for me

Code:
awk -F - -vDT="$(date --date="30 minutes ago" "+%b %_d %H:%M:%S")" ' DT < $1' log.file >tmp.txt

log file time format is 2016-09-27 14:00:25,192


Moderator's Comments:
Mod Comment Use code tags, thanks.

Last edited by zaxxon; 09-28-2016 at 07:21 AM..
# 2  
Old 09-28-2016
Quote:
Originally Posted by Prashanth.K
Hi All,

I want to read the log file for last 30mins logs with time stamps.

Am using below command but, it is not working for me

Code:
awk -F - -vDT="$(date --date="30 minutes ago" "+%b %_d %H:%M:%S")" ' DT < $1' log.file >tmp.txt

log file time format is 2016-09-27 14:00:25,192


Moderator's Comments:
Mod Comment Use code tags, thanks.
So you have a log file with a date an time format as produced by the date command:
Code:
date "+%Y-%m-%d %H:%M:%S"

which give you a string similar to:
Code:
2016-09-28 05:19:09

and you extract the date field from this string to be just the year by using
Code:
awk -F- '$1'

and then you compare that to a string produced by the date command:
Code:
date "+%b %_d %H:%M:%S"

which gives you output similar to:
Code:
Sep 28 05:19:09

which means that your awk command:
Code:
awk -F - -vDT="$(date --date="30 minutes ago" "+%b %_d %H:%M:%S")" ' DT < $1' log.file

boils down to something like:
Code:
awk -F - -vDT="Sep 28 04:49:09" ' DT < $1 log.file'

which will print every line in log.file for which the expression:
Code:
"Sep 28 04:49:09" < 2016

evaluates to true (assuming that the date field in log.file starts in column 1 in each line).

Without knowing the format of log.file (specifically where the date appears on each line in that file and what field separator is used to separate the dates in that file from other text in that file, we can't help you solve your general problem. But we do know that you need to use the same format for the strings you are comparing and that you need to compare the same number of fields (i.e., year, month, day, hour, minute, and second need to be compared to year, month, day, hour, minute, and second). Comparing abbreviated month name, day, hour, minute, and second to year cannot possibly select an appropriate 30 minute period.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

2. Red Hat

PAM: Unlock user account after 30mins

Hi We have these specific requirements for a bunch of servers we have and cannot seem to get pam to behave in this way. We would like: PAM locks accounts if pam tally reaches 10. PAM unlocks the account after 30mins from locking it, and resets the pam_tally. The key is that we don't... (0 Replies)
Discussion started by: snoop2048
0 Replies

3. Shell Programming and Scripting

Script to check logs

I have 5 log files under different directores . say for eg abc under /home/dir1 , xyz under home/dir2 . is there a script that i can run from say /home that searchers all these files for string or combination of strings and write to a file eg search file by timestamp|keyword o/p in a file (6 Replies)
Discussion started by: Nevergivup
6 Replies

4. Shell Programming and Scripting

script to constantly read the last 500 new logs in a log file

Hello, I would like to write a bash script that would monitor a log file for a certain number of logs, let's say 500 logs and when it reaches that number to write the last log to another file. For example, I want to watch the /var/adm/messages and everytime, there is 500 new logs that are... (1 Reply)
Discussion started by: Pouchie1
1 Replies

5. Shell Programming and Scripting

script for reading logs of a script running on other UNIX server

Hi, I have a script, running on some outside firwall server and it's log of success or failure is maintained in a file. I want to write a script which ftp that server and reads that file and checks the logs and if failure , I will send mail notification. Please let meknow if I am not... (1 Reply)
Discussion started by: vandana.parwani
1 Replies

6. UNIX for Dummies Questions & Answers

gzip to read logs

Ok here is my trouble. I run scripts whenever I am investigating problems. One of those scripts simply goes to a log folder and runs gzip -cd * | grep error | wc -l This tells me the number of errors in their log's and how much I should pay attention to them. The problem is sometimes... (3 Replies)
Discussion started by: MrEddy
3 Replies

7. Shell Programming and Scripting

help using read in menu script to cat out lines in logs

What is wrong with my menu script? Do I need to continue with the read statements? All I want to do with option 4 is to cat some /var/log/files and awk out a few lines? How do I do that please? $ cat menu.sh ... (11 Replies)
Discussion started by: taekwondo
11 Replies

8. Shell Programming and Scripting

Grep yesterday logs from weblogic logs

Hi, I am trying to write a script which would go search and get the info from the logs based on yesterday timestamp and write yesterday logs in new file. The log file format is as follows: """"""""""""""""""""""""""... (3 Replies)
Discussion started by: harish.parker
3 Replies

9. Shell Programming and Scripting

Run a script on the hour but only for 30mins

Hi All, I want to run a script on the hour during a 24 - hour period; easy enough cron will take care of that..however I want the script to only run for only 30mins.. so with the script it knows its 30mins are up so exits. any ideas? Any help, greatly appericated. Thanking you all... (2 Replies)
Discussion started by: Zak
2 Replies

10. Shell Programming and Scripting

find files older than 30mins,count and send mail

Hi all, I wrote this script to find files older than time parameter, count the number of files, and send an email to me that some files are in a particular folder. For the particular path, the script should wait delay parameter before running again. For example, assuming the input file looks... (4 Replies)
Discussion started by: kayarsenal
4 Replies
Login or Register to Ask a Question