For Programmers: Free Programming Magazines  


Home > Archive > AWK > April 2007 > Date comparison in flat file









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 Date comparison in flat file
Meshach

2007-03-07, 3:57 am

Dear all,

I am having a file format like

2007-01-15 16:04:44 73125
2007-01-25 14:25:08 74302
2007-02-02 06:04:02 78580
2007-02-02 10:28:32 78614
2007-02-05 11:00:12 78800
2007-02-08 03:45:51 79184
2007-02-14 13:40:57 79805
2007-02-16 09:37:16 80006
2007-02-16 10:37:33 80017
2007-02-19 15:34:21 80209
2007-02-20 10:16:49 80271
2007-02-21 21:00:09 80525
2007-02-22 11:56:40 80569
2007-02-23 15:45:58 80706
2007-02-23 21:09:11 80724
2007-02-27 11:02:45 88726
2007-02-27 11:38:57 88733
2007-03-01 12:17:55 89060
2007-03-01 12:25:45 89061
2007-03-01 15:09:36 89098
2007-03-01 20:40:16 89136
2007-03-02 09:45:51 90240
2007-03-02 10:25:09 90477
2007-03-05 11:08:56 90657
2007-03-05 11:22:40 90659
2007-03-05 11:22:48 90660
2007-03-05 15:01:14 90698
2007-03-05 15:26:15 90705
2007-03-06 06:45:52 90770
2007-03-06 06:59:48 90773
2007-03-06 11:11:21 90806
2007-03-06 11:30:47 90811
2007-03-06 11:57:41 90822
2007-03-06 11:57:50 90820
2007-03-06 13:21:35 90834
2007-03-06 13:33:37 90838
2007-03-06 13:49:38 90840
2007-03-06 14:57:58 90848
2007-03-06 15:49:15 90858
2007-03-06 16:22:12 90864
2007-03-06 16:45:12 90868
2007-03-06 17:03:49 90871
2007-03-06 17:48:13 90879
2007-03-06 19:15:02 90885


How can I list the record between 2007-02-02(yy-mm-dd) and 2007-03-06
using awk or perl

Thomas Weidenfeller

2007-03-07, 3:57 am

Meshach wrote:
> Dear all,
>
> I am having a file format like
>
> 2007-01-15 16:04:44 73125
> 2007-01-25 14:25:08 74302


<...>
>
>
> How can I list the record between 2007-02-02(yy-mm-dd) and 2007-03-06
> using awk


/^2007-03-06/ { exit }
/^2007-02-02/ { y++ }
y { print }

> or perl


Wrong group :-)

/Thomas
Kenny McCormack

2007-03-07, 8:03 am

In article <esls5n$uql$1@news.al.sw.ericsson.se>,
Thomas Weidenfeller <nobody@ericsson.invalid> wrote:
>Meshach wrote:
>
><...>
>
>/^2007-03-06/ { exit }
>/^2007-02-02/ { y++ }
>y { print }


The obvious shot is:

/^2007-03-06/,/^2007-02-02/

But then teh queston becomes "What does 'between' mean?" Does it
include the "edges"? If not, you can still do it this way, but you have
to include code to handle the edges. I've done it; it's not that hard.

>
>Wrong group :-)


As are C, assembler, Fortran, and Python (to name a few).

Vassilis

2007-03-07, 6:57 pm


=CF/=C7 Meshach =DD=E3=F1=E1=F8=E5:
> Dear all,
>
> I am having a file format like
>
> 2007-01-15 16:04:44 73125
> 2007-01-25 14:25:08 74302
> 2007-02-02 06:04:02 78580
> 2007-02-02 10:28:32 78614
> 2007-02-05 11:00:12 78800
> 2007-02-08 03:45:51 79184
> 2007-02-14 13:40:57 79805
> 2007-02-16 09:37:16 80006
> 2007-02-16 10:37:33 80017
> 2007-02-19 15:34:21 80209
> 2007-02-20 10:16:49 80271
> 2007-02-21 21:00:09 80525
> 2007-02-22 11:56:40 80569
> 2007-02-23 15:45:58 80706
> 2007-02-23 21:09:11 80724
> 2007-02-27 11:02:45 88726
> 2007-02-27 11:38:57 88733
> 2007-03-01 12:17:55 89060
> 2007-03-01 12:25:45 89061
> 2007-03-01 15:09:36 89098
> 2007-03-01 20:40:16 89136
> 2007-03-02 09:45:51 90240
> 2007-03-02 10:25:09 90477
> 2007-03-05 11:08:56 90657
> 2007-03-05 11:22:40 90659
> 2007-03-05 11:22:48 90660
> 2007-03-05 15:01:14 90698
> 2007-03-05 15:26:15 90705
> 2007-03-06 06:45:52 90770
> 2007-03-06 06:59:48 90773
> 2007-03-06 11:11:21 90806
> 2007-03-06 11:30:47 90811
> 2007-03-06 11:57:41 90822
> 2007-03-06 11:57:50 90820
> 2007-03-06 13:21:35 90834
> 2007-03-06 13:33:37 90838
> 2007-03-06 13:49:38 90840
> 2007-03-06 14:57:58 90848
> 2007-03-06 15:49:15 90858
> 2007-03-06 16:22:12 90864
> 2007-03-06 16:45:12 90868
> 2007-03-06 17:03:49 90871
> 2007-03-06 17:48:13 90879
> 2007-03-06 19:15:02 90885
>
>
> How can I list the record between 2007-02-02(yy-mm-dd) and 2007-03-06
> using awk or perl



Using ISO dates has the added benefit that you can treat them as
numbers.

{ split($1, a, "-"); date =3D a[1] a[2] a[3] }
date >=3D 20070202 && date <=3D 20070306

And you can use it in case your input isn't already sorted.

Ed Morton

2007-03-08, 6:57 pm

Meshach wrote:

> Dear all,
>
> I am having a file format like
>
> 2007-01-15 16:04:44 73125
> 2007-01-25 14:25:08 74302
> 2007-02-02 06:04:02 78580
> 2007-02-02 10:28:32 78614
> 2007-02-05 11:00:12 78800
> 2007-02-08 03:45:51 79184
> 2007-02-14 13:40:57 79805
> 2007-02-16 09:37:16 80006
> 2007-02-16 10:37:33 80017
> 2007-02-19 15:34:21 80209
> 2007-02-20 10:16:49 80271
> 2007-02-21 21:00:09 80525
> 2007-02-22 11:56:40 80569
> 2007-02-23 15:45:58 80706
> 2007-02-23 21:09:11 80724
> 2007-02-27 11:02:45 88726
> 2007-02-27 11:38:57 88733
> 2007-03-01 12:17:55 89060
> 2007-03-01 12:25:45 89061
> 2007-03-01 15:09:36 89098
> 2007-03-01 20:40:16 89136
> 2007-03-02 09:45:51 90240
> 2007-03-02 10:25:09 90477
> 2007-03-05 11:08:56 90657
> 2007-03-05 11:22:40 90659
> 2007-03-05 11:22:48 90660
> 2007-03-05 15:01:14 90698
> 2007-03-05 15:26:15 90705
> 2007-03-06 06:45:52 90770
> 2007-03-06 06:59:48 90773
> 2007-03-06 11:11:21 90806
> 2007-03-06 11:30:47 90811
> 2007-03-06 11:57:41 90822
> 2007-03-06 11:57:50 90820
> 2007-03-06 13:21:35 90834
> 2007-03-06 13:33:37 90838
> 2007-03-06 13:49:38 90840
> 2007-03-06 14:57:58 90848
> 2007-03-06 15:49:15 90858
> 2007-03-06 16:22:12 90864
> 2007-03-06 16:45:12 90868
> 2007-03-06 17:03:49 90871
> 2007-03-06 17:48:13 90879
> 2007-03-06 19:15:02 90885
>
>
> How can I list the record between 2007-02-02(yy-mm-dd) and 2007-03-06
> using awk or perl
>


If the dates are guaranteed to be sorted as above, see Kenny's post,
otherwise:

gsub(/-/,"") && $1 >= 20070202 && $1 <= 20070306

Regards,

Ed.
Kurupt

2007-03-17, 6:03 am

Britney Spears Spreading Outdoors!
http://Britney-Spears-Spreading-Out...hp?movie=148803
Mag Gam

2007-03-24, 9:57 pm

On Mar 8, 10:25 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> Meshach wrote:
>
>
>
>
> If the dates are guaranteed to be sorted as above, see Kenny's post,
> otherwise:
>
> gsub(/-/,"") && $1 >= 20070202 && $1 <= 20070306
>
> Regards,
>
> Ed.


I tried this:
awk '/2007-02-02/ || /2007-03-06/ { print }' file

What is the difference between || and , (comma) ?

(still learning Unix + awk)

Janis Papanagnou

2007-03-25, 3:56 am

Mag Gam wrote:
> On Mar 8, 10:25 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
> I tried this:
> awk '/2007-02-02/ || /2007-03-06/ { print }' file
>
> What is the difference between || and , (comma) ?


The former d1||d2 matches any lines that contain either of the two dates,
d1 or d2.

The latter d1,d2 matches all lines starting from lines containing d1 and
ending on a line with d2; it specifies a _range_ where the two bounds are
defined by the patterns (there may be multiple blocks of data that match).

Janis

>
> (still learning Unix + awk)
>

Joseph

2007-03-25, 6:56 pm

On Mar 7, 2:39 am, "Meshach" <a.mesh...@gmail.com> wrote:
> Dear all,
>
> I am having a file format like
>
> 2007-01-15 16:04:44 73125
> 2007-01-25 14:25:08 74302
> 2007-02-02 06:04:02 78580
> 2007-02-02 10:28:32 78614
> 2007-02-05 11:00:12 78800
> 2007-02-08 03:45:51 79184
> 2007-02-14 13:40:57 79805
> 2007-02-16 09:37:16 80006
> 2007-02-16 10:37:33 80017
> 2007-02-19 15:34:21 80209
> 2007-02-20 10:16:49 80271
> 2007-02-21 21:00:09 80525
> 2007-02-22 11:56:40 80569
> 2007-02-23 15:45:58 80706
> 2007-02-23 21:09:11 80724
> 2007-02-27 11:02:45 88726
> 2007-02-27 11:38:57 88733
> 2007-03-01 12:17:55 89060
> 2007-03-01 12:25:45 89061
> 2007-03-01 15:09:36 89098
> 2007-03-01 20:40:16 89136
> 2007-03-02 09:45:51 90240
> 2007-03-02 10:25:09 90477
> 2007-03-05 11:08:56 90657
> 2007-03-05 11:22:40 90659
> 2007-03-05 11:22:48 90660
> 2007-03-05 15:01:14 90698
> 2007-03-05 15:26:15 90705
> 2007-03-06 06:45:52 90770
> 2007-03-06 06:59:48 90773
> 2007-03-06 11:11:21 90806
> 2007-03-06 11:30:47 90811
> 2007-03-06 11:57:41 90822
> 2007-03-06 11:57:50 90820
> 2007-03-06 13:21:35 90834
> 2007-03-06 13:33:37 90838
> 2007-03-06 13:49:38 90840
> 2007-03-06 14:57:58 90848
> 2007-03-06 15:49:15 90858
> 2007-03-06 16:22:12 90864
> 2007-03-06 16:45:12 90868
> 2007-03-06 17:03:49 90871
> 2007-03-06 17:48:13 90879
> 2007-03-06 19:15:02 90885
>
> How can I list the record between 2007-02-02(yy-mm-dd) and 2007-03-06
> using awk or perl


(("2007-02-02"<=$1)&&($1<="2007-03-06")){print;}

Super

2007-04-04, 5:18 am

Britney Spears Spreading Outdoors!
http://Britney-Spears-Spreading-Out...hp?movie=148803
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com