Sponsored Content
Top Forums Shell Programming and Scripting Retrieve logs generated in last 10 mins from a log file using 'grep' command Post 302525310 by ctsgnb on Thursday 26th of May 2011 10:52:39 AM
Old 05-26-2011
... just an example, the code is not very strict and may sometimes display more than what it is supposed to, but it may still give some idea :

The XX and YY offset (**) may need to be adjusted depending on the format of your date

**see : cut -cXX and substr(...,YY,...)

Note that the tr translation may also need to be adjusted to your needs depending on which column you want to shift (minute? hours? other ?) as well as the interval (5 min? 7min ? 10min?)...

Here an example for the last 5 min (not exactly in fact... more 4 min than 5 but you get the idea)

Code:
[ctsgnb@shell ~/sand]$ cat tst
Jul 19 18:15:23 ........
Jul 19 18:15:28 ........
Jul 19 18:17:11 ........
Jul 19 18:19:11 ........
Jul 19 18:20:23 ........
Jul 19 18:25:28 ........
Jul 19 18:26:11 ........
Jul 19 18:27:11 ........
Jul 19 18:28:23 ........
Jul 19 18:29:28 ........
Jul 19 18:30:11 ........
Jul 19 18:34:11 ........
Jul 19 18:40:23 ........
Jul 19 18:41:28 ........
Jul 19 18:42:11 ........
Jul 19 18:43:11 ........

[ctsgnb@shell ~/sand]$ date ; m=$(date | cut -c16 | tr '0123456789' '5678901234') ; echo $m
Thu May 26 08:23:22 MDT 2011
8
[ctsgnb@shell ~/sand]$ tail -r tst | awk -v M="$m" 'substr($0,12,1)==M{print;x=1}!x' | tail -r
Jul 19 18:28:23 ........
Jul 19 18:29:28 ........
Jul 19 18:30:11 ........
Jul 19 18:34:11 ........
Jul 19 18:40:23 ........
Jul 19 18:41:28 ........
Jul 19 18:42:11 ........
Jul 19 18:43:11 ........

[ctsgnb@shell ~/sand]$

---------- Post updated at 04:52 PM ---------- Previous update was at 04:38 PM ----------

A very little improvement :

Code:
m=$(date | cut -c16 | tr '0123456789' '4567890123')
tail -r tst | awk -v M="$m" 'substr($0,12,1)==M{x=1}!x' | tail -r

so that if there are more than 1 consecutive entry that have ((current_minute) - (5min)), they are all displayed instead of just the first one

Code:
[ctsgnb@shell ~/sand]$ cat tst
Jul 19 18:15:23 ........
Jul 19 18:15:28 ........
Jul 19 18:17:11 ........
Jul 19 18:19:11 ........
Jul 19 18:20:23 ........
Jul 19 18:25:28 ........
Jul 19 18:26:11 ........
Jul 19 18:27:11 ........
Jul 19 18:28:23 ........
Jul 19 18:29:28 ........
Jul 19 18:30:11 ........
Jul 19 18:34:11 ........
Jul 19 18:40:23 ........
Jul 19 18:40:23 ........
Jul 19 18:41:28 ........
Jul 19 18:42:11 ........
Jul 19 18:42:11 ........
Jul 19 18:43:11 ........

[ctsgnb@shell ~/sand]$ date ; m=$(date | cut -c16 | tr '0123456789' '4567890123') ; echo $m
Thu May 26 08:47:56 MDT 2011
1
[ctsgnb@shell ~/sand]$ tail -r tst | awk -v M="$m" 'substr($0,12,1)==M{x=1}!x' | tail -r
Jul 19 18:42:11 ........
Jul 19 18:42:11 ........
Jul 19 18:43:11 ........

[ctsgnb@shell ~/sand]$

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

grep command to retrieve one file

The Sed/Grep command is really confusing me. I know I'm missing something that should be really easy to fix. My program displays multiple names after I ask it to display only one, How do I get it to do only one?? it looks like this: Please enter a name to display? >> John (A list then... (9 Replies)
Discussion started by: toejam
9 Replies

2. Shell Programming and Scripting

Retrieve logs for last 4 hours

Our log file looks like this: 2010-11-18 00:57:01,698 ERROR Shipment Error Log:Error Details - Account Id:3962, PO:2710015, Line:2, File:221112901, Version:V1, Desc:Order cannot not be shipped as there are no line items in New state 2010-11-18 14:59:39,960 ERROR... (11 Replies)
Discussion started by: Deepthz
11 Replies

3. Shell Programming and Scripting

Retrieve logs for previous 4 hours

Hi, I am in the process of configuring a script, and i intend it to retrieve logs for previous four hours, and then scan for predefined errors. I am kind of stuck on the log retrieval part where the script will run early morning like 1 AM or 2 AM, the command as posted below will give me... (4 Replies)
Discussion started by: john_prince
4 Replies

4. Shell Programming and Scripting

Script to Grep column 3 from csv file generated yesterday

Hello, Can any one please assist how to scirpt it: Every day a new log file is create and I want to process only the one generated yesterday and get the data of column 3 and 6. For example today's date is 24 then I want to get the data of log file created on 23rd. Log Files in... (7 Replies)
Discussion started by: sureshcisco
7 Replies

5. AIX

Grep last 5 mins from log file in AIX

I want to grep only last 5 mins of a log file in bash I have a syslog which contains the following Mon Jul 11 20:47:42 Mon Jul 11 20:47:52 The following works in Unix but not in AIX . Please can you let me know as to what would be the AIX equivalent Code: for (( i = 5; i >=0;... (1 Reply)
Discussion started by: necro98
1 Replies

6. Shell Programming and Scripting

Script to grep for a string in log files generated in last 15 minutes.

Dear Guru's I've a requirment to grep for a string in series of log files that are getting generated almost every minute. I'm looking to schedule a script every 15 mountes,in order to check if the error string has been generated in any of the log files generated in last 15 minutes. Please... (3 Replies)
Discussion started by: rajivatnova
3 Replies

7. UNIX for Dummies Questions & Answers

Script to search log file for last 15 mins data

Hi All, I have an issue which I'm trying to understand a way of doing, I have several nodes which contain syslog events which I want to force trigger an email initially (eventually leading to another method of alerting but to start with an email). Basically the syslog file will have hours worth... (6 Replies)
Discussion started by: mutley2202
6 Replies

8. Shell Programming and Scripting

Need logs 5 mins old

I need 5 mins old logs to be dumped into a a new file. The date formats in the two log files are Can you suggect for both formats ? bash-3.2$ uname -a SunOS myserver 5.10 Generic_150400-26 sun4v sparc sun4v ---------- Post updated 05-04-16 at 12:24 AM ---------- Previous update was... (2 Replies)
Discussion started by: mohtashims
2 Replies

9. UNIX for Beginners Questions & Answers

Grep: Retrieve two strings from one file to find them anyone on line in another file

I am having trouble matching *two* strings from one file anywhere in a line of a second file, and could use some help getting this figured out. My preference would be to use grep for this because I would like to take advantage of its -A option. The latter is due to the fact that I would like both... (2 Replies)
Discussion started by: jvoot
2 Replies

10. UNIX for Beginners Questions & Answers

Grep last 5 mins from logs

Hi, system date format Thu Jun 13 12:55:18 EDT 2019 My log date format 09.148.192.60 - - "GET /akamai/sureroute-test-object.html HTTP/1.1" 404 231 can someone please help me, how to get last 5mins of logs please ? I need the command Please wrap your samples/codes in CODE TAGS,... (3 Replies)
Discussion started by: scazed
3 Replies
shell(1F)							   FMLI Commands							 shell(1F)

NAME
shell - run a command using shell SYNOPSIS
shell command [command] ... DESCRIPTION
The shell function concatenate its arguments, separating each by a space, and passes this string to the shell ($SHELL if set, otherwise /usr/bin/sh). EXAMPLES
Example 1: A sample output of shell command. Since the Form and Menu Language does not directly support background processing, the shell function can be used instead. `shell "build prog > /dev/null &"` If you want the user to continue to be able to interact with the application while the background job is running, the output of an exe- cutable run by shell in the background must be redirected: to a file if you want to save the output, or to /dev/null if you don't want to save it (or if there is no output), otherwise your application may appear to be hung until the background job finishes processing. shell can also be used to execute a command that has the same name as an FMLI built-in function. NOTES
The arguments to shell will be concatenate using spaces, which may or may not do what is expected. The variables set in local environments will not be expanded by the shell because "local" means "local to the current process." ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
sh(1), attributes(5) SunOS 5.10 5 Jul 1990 shell(1F)
All times are GMT -4. The time now is 10:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy