Unix questions - How to choose only lines between specific date till now?

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > January 2006 > How to choose only lines between specific date till now?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to choose only lines between specific date till now?
T0me3

2006-01-21, 7:49 am

Hello,

I have log file with entries:

2005-10-1 Server error at......
exception at
2005-10-2 Starting something
Something started
Client requested ...........
2005-10-3 Server encountered error at.....
Something happended


I would like to print only lines containing errors, but only those,
which are from 2005-10-3 or later.
How can i achieve this? grep -i -e "error" myfile.txt prints all lines
with error, but i would like to print
only lines with errors from date 2005-10-3 to today.

Thanks in advance

T0me3

Chris F.A. Johnson

2006-01-22, 6:11 pm

On 2006-01-21, T0me3 wrote:
> Hello,
>
> I have log file with entries:
>
> 2005-10-1 Server error at......
> exception at
> 2005-10-2 Starting something
> Something started
> Client requested ...........
> 2005-10-3 Server encountered error at.....
> Something happended
>
>
> I would like to print only lines containing errors, but only those,
> which are from 2005-10-3 or later.
> How can i achieve this? grep -i -e "error" myfile.txt prints all lines
> with error, but i would like to print
> only lines with errors from date 2005-10-3 to today.


Assuming that the lines are in chronological order:

awk '/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9]/ {
split($1,d,"-")
date = sprintf("%04d%02d%02d", d[1], d[2], d[3])
}
/error/ && date > 20051003 { print }
'

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Michael Paoli

2006-01-22, 6:11 pm

T0me3 wrote:
> I have log file with entries:
>
> 2005-10-1 Server error at......
> exception at
> 2005-10-2 Starting something
> Something started
> Client requested ...........
> 2005-10-3 Server encountered error at.....
> Something happended
>
> I would like to print only lines containing errors, but only those,
> which are from 2005-10-3 or later.


If your log entries are chronological, how about something like:
sed -ne '/^2005-10-3/,${/error/p;}'

One can also make the part within {} more complex to handle, e.g.
multiple regular expressions, e.g.:
sed -ne '/^2005-10-3/,${/error/{p;n;};/fail/p;}'

Stephane CHAZELAS

2006-01-23, 2:55 am

2006-01-21, 03:41(-08), T0me3:
> Hello,
>
> I have log file with entries:
>
> 2005-10-1 Server error at......
> exception at
> 2005-10-2 Starting something
> Something started
> Client requested ...........
> 2005-10-3 Server encountered error at.....
> Something happended
>
>
> I would like to print only lines containing errors, but only those,
> which are from 2005-10-3 or later.
> How can i achieve this? grep -i -e "error" myfile.txt prints all lines
> with error, but i would like to print
> only lines with errors from date 2005-10-3 to today.

[...]

I would suggest that you modify your application that logs those
lines so that it formats the time stamps as 2005-10-03 instead.
This way, a string comparison would be enough:

awk '
$1 "" > "2005-10-03" && /error/' < file.log


--
Stéphane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com