From: shulamitm on
Hi All,

I have a file with the folloing format:
<date> <time> <message>
and I need to get all the lines from a given date < e.g. 2010-04-27>
and a given time and up <e.g. 03:20:00>

For example:

I have the following file:

2010-04-19 07:27:56.795 Error.
2010-04-27 02:19:04.158 2 message.
2010-04-27 03:01:04.158 1 message.
2010-04-27 03:19:04.158 1 message.
2010-04-27 03:23:07.158 3 message.
2010-04-27 03:31:13.110 expired

and for the given details 2010-04-27, 03:20:00 I want to get the
following lines:

2010-04-27 03:23:07.158 Expired 1 message.
2010-04-27 03:31:13.110 Route 'CLKP' disconnected, connection
id=243949


could you please advice?

thanks!
From: Janis Papanagnou on
Sorry, forget my previous posting. No coffee, yet.

Janis Papanagnou wrote:
>
> Since you use ISO dates this should do
>
> awk '$1>="2010-04-27" && $2>="03:20' your_file

awk '$1>"2010-04-27" || ($1="2010-04-27" && $2>="03:20:00)' your_file


>
> If you want to use shell variables to pass date and time, e.g. if
> embedding the awk command in a script
>
> awk -v d=${1:?} -v t=${2:?} '$1>=d && $2>=t' your_file

awk -v d=${1:?} -v t=${2:?} '$1>d || ($1=d && $2>=t)' your_file


(Hope I've got it right now.)

>
>
> Janis
>
>> thanks!
From: shulamitm on
On May 3, 11:10 am, Janis Papanagnou <janis_papanag...(a)hotmail.com>
wrote:
> shulamitm wrote:
> > Hi All,
>
> > I have a file with the folloing format:
> > <date> <time> <message>
> > and I need to get all the lines from a given date < e.g. 2010-04-27>
> > and a given time and up <e.g. 03:20:00>
>
> > For example:
>
> > I have the following file:
>
> > 2010-04-19 07:27:56.795 Error.
> > 2010-04-27 02:19:04.158 2 message.
> > 2010-04-27 03:01:04.158 1 message.
> > 2010-04-27 03:19:04.158 1 message.
> > 2010-04-27 03:23:07.158 3 message.
> > 2010-04-27 03:31:13.110 expired
>
> > and for the given details 2010-04-27, 03:20:00 I want to get the
> > following lines:
>
> > 2010-04-27 03:23:07.158 Expired 1 message.
> > 2010-04-27 03:31:13.110 Route 'CLKP' disconnected, connection
> > id=243949
>
> > could you please advice?
>
> Since you use ISO dates this should do
>
>   awk '$1>="2010-04-27" && $2>="03:20' your_file
>
> If you want to use shell variables to pass date and time, e.g. if
> embedding the awk command in a script
>
>   awk -v d=${1:?} -v t=${2:?} '$1>=d && $2>=t' your_file
>
> Janis
>
>
>
>
>
> > thanks!- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

thank you very much!
From: Ben Bacarisse on
Janis Papanagnou <janis_papanagnou(a)hotmail.com> writes:

> Sorry, forget my previous posting. No coffee, yet.
>
> Janis Papanagnou wrote:
>>
>> Since you use ISO dates this should do
>>
>> awk '$1>="2010-04-27" && $2>="03:20' your_file
>
> awk '$1>"2010-04-27" || ($1="2010-04-27" && $2>="03:20:00)' your_file

Typo: the = should be ==, of course.

>> If you want to use shell variables to pass date and time, e.g. if
>> embedding the awk command in a script
>>
>> awk -v d=${1:?} -v t=${2:?} '$1>=d && $2>=t' your_file
>
> awk -v d=${1:?} -v t=${2:?} '$1>d || ($1=d && $2>=t)' your_file

Ditto.

There's a strong case for simply using:

$0 >= "2010-04-27 03:20:00"

as the test (suitably configured to use the date in $1). For one thing,
it lets one specify as much or as little precision as one likes.

--
Ben.
From: Janis Papanagnou on
Ben Bacarisse schrieb:
> Janis Papanagnou <janis_papanagnou(a)hotmail.com> writes:
>> [...]
>
> Typo: the = should be ==, of course.

Thanks for catching that.

>>> If you want to use shell variables to pass date and time, e.g. if
>>> embedding the awk command in a script
>>>
>>> awk -v d=${1:?} -v t=${2:?} '$1>=d && $2>=t' your_file
>> awk -v d=${1:?} -v t=${2:?} '$1>d || ($1=d && $2>=t)' your_file
>
> Ditto.
>
> There's a strong case for simply using:
>
> $0 >= "2010-04-27 03:20:00"
>
> as the test (suitably configured to use the date in $1). For one thing,
> it lets one specify as much or as little precision as one likes.

The precicion issue is also covered by the preceding suggestion.
But you have a point that in this case (where the date information
starts with field 1) it can as well be simplified as you proposed.

Janis