Sponsored Content
Full Discussion: Extract data from log
Top Forums Shell Programming and Scripting Extract data from log Post 302937302 by Don Cragun on Thursday 5th of March 2015 12:18:36 AM
Old 03-05-2015
Quote:
Originally Posted by oberoi1403
I have logs in format
Code:
  ####<01-Mar-2015 03:48:18 o'clock GMT> <info>
 ####<01-Mar-2015 03:48:20 o'clock GMT> <info>
####<01-Mar-2015 03:48:30 o'clock GMT> <info>
####<01-Mar-2015 03:48:39 o'clock GMT> <info>

I am looking out for a script which can extract data of last 15 minutes from the last recorded data in the log file and then search a string in it and store the results in a file.
Additionally, I tried the below command but doesn't work for me
Code:
awk '$0>=$from' from=$(`date -u +"####<%d-%b-%Y %H:%M:%S o'clock GMT>" -d -5min`) test.log | grep -m1 -C5 'WORD'

The command you tried can't work for at least the following reasons (there may be others):
  1. when comparing dates, you need to compare year, month, and day (in that order); not day, month, and year,
  2. the month names Jan, Feb, ... do not sort into increasing date order,
  3. the dates in your log file are based on GMT, but the dates you are producing with the date command are based on the current process' TZ setting,
  4. some of your input lines have leading spaces,
  5. you have a command substitution inside a command substitution causing the from variable to be set to an empty string (and it should also generate a diagnostic similar to -ksh: ####<01-Mar-2015: not found that you didn't bother mentioning),
  6. the value you seem to be trying to store into the from variable is not a field number, and
  7. from what you have shown us, the string WORD does not appear anywhere in the input you are processing.
There is no need to convert the timestamps to seconds since the Epoch as long as you convert the date into a string that will sort correctly when doing a string comparison (i.e., 20150301 instead of 15-Jan-2015), and awk is certainly capable of converting the date portion of the format in your log file into the format above before comparing the results to the string produced by the command:
Code:
from="$(date +"###<%Y%m%d %H:%M:%S o'clock GMT>")"

.
If we assume that <info> is shorthand for some kind of information being logged in your log files, and if <info> NEVER contains a <newline> character, and if <info> in some of those line contain the string WORD, then the following might do what you want:
Code:
awk -v from=$(date -u -d '-15 min' +'%Y%m%d%H:%M:%S') -v debug="$debug" '
BEGIN {	m["Jan"] = "01"; m["Feb"] = "02"; m["Mar"] = "03"; m["Apr"] = "04"
	m["May"] = "05"; m["Jun"] = "06"; m["Jul"] = "07"; m["Aug"] = "09"
	m["Sep"] = "09"; m["Oct"] = "10"; m["Nov"] = "11"; m["Dec"] = "12"
}
split($1, mdy, /[-<]/) == 4 {
		mod_date = mdy[4] m[mdy[3]] mdy[2] $2
		if(debug) printf("mod_date=%s, from=%s, $1=%s, $2=%s\n",
			mod_date, from, $1, $2) > "debug.out"
}
mod_date >= from
' test.log | grep -m1 -C3 'WORD'

Note that this is untested because the date utility on my system does not have a -d option; AND, you didn't supply any sample data that would produce any output from your sample input.

The way this is coded, if the <info> data in your input files does contain multi-line data, it will copy the entire <info> field to the output if the 1st line meets the date requirements as long as no line in your input data contains exactly three < and or - characters in the first field of a line that is not in the specified format.
These 2 Users Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help: Log data extract

So I got this old webapp doing some strange format with its logging and I need to extract only specific parts. Basically the format of the log goes: Date:<space>timestamp User:<space>userid AppID:<space>applicationid Duration:<space>time(ms) <line> <line> Date:<space>timestamp... (1 Reply)
Discussion started by: kenm0j0
1 Replies

2. Shell Programming and Scripting

How to extract last week data from a log

HI, I have a log file (sync.log) in the following format. 05-14 05:34:56 INFO Hxxx Start: Id:xxx 05-14 05:45:32 INFO Hxxx End: Id:xxxx 05-19 11:23:21 INFO Hxxx Start: Id:xxx 05-19 11:34:45 INFO Hxxx End: Id:xxxx 05-20 02:09:47 INFO Hxxx Start: Id:xxx 05-20 02:36:03 INFO Hxxx End: Id:xxx... (5 Replies)
Discussion started by: praveenvi
5 Replies

3. Shell Programming and Scripting

shell-script which extract data from log file

give me a shell-script which extract data from log file on a server by giving date and time as input (for both start time and end time) and it will give the logs generated during the given time as output. (4 Replies)
Discussion started by: abhishek27
4 Replies

4. Shell Programming and Scripting

Extract data from log file from or after the specific date

Hi , I am having a script which will start a process and appends the process related logs to a log file. The log file writes logs with every line starting with date in the format of: date +"%Y %b %d %H:%M:%S". So, in the script, before I start the process, I am storing the date as DATE=`date +"%Y... (5 Replies)
Discussion started by: chiru_h
5 Replies

5. Shell Programming and Scripting

need a shell script to extract data from a log file.

If I have a log like : Mon Jul 19 05:07:34 2010; TCP; eth3; 52 bytes; from abc to def Mon Jul 19 05:07:35 2010; UDP; eth3; 46 bytes; from aaa to bbb Mon Jul 19 05:07:35 2010; TCP; eth3; 52 bytes; from def to ghi I will need an output like this : Time abc to def... (1 Reply)
Discussion started by: hitha87
1 Replies

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

7. Shell Programming and Scripting

Data Extract from XML Log File

Please help me out to extract the Data from the XML Log files. So here is the data ERROR|2010-08-26 00:05:52,958|SERIAL_ID=128279996|ST=2010-08-2600:05:52|DEVICE=113.2.21.12:601|TYPE=TransactionLog... (9 Replies)
Discussion started by: raghunsi
9 Replies

8. Shell Programming and Scripting

Extract data from log file in specified range of time

I was searching for parsing a log file and found what I need in this link http://stackoverflow.com/questions/7575267/extract-data-from-log-file-in-specified-range-of-time But the most useful answer (posted by @Kent): # this variable you could customize, important is convert to seconds. # e.g... (2 Replies)
Discussion started by: kingk110
2 Replies

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

10. Shell Programming and Scripting

Extract data from log file within specified time

So, we have a script, that is supposed to have a couple of functions like showing number of failed connections, recieved bytes per IP-address, and so on. We are supposed to be able to limit the number of results to either 0-24 hours or X days back from the last data in the log file. Everything... (3 Replies)
Discussion started by: Plumpen
3 Replies
All times are GMT -4. The time now is 10:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy