For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2004 > filter on dates









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 filter on dates
AJ Norman

2004-03-26, 11:09 pm

Hi,
I have a datafile which contains two date columns.
I'm using the following logic to create two new files base on these dates.
It seems straight forward enough but I can't seem to get it to work.

awk 'BEGIN{
spcs=" "
}
{
f = substr($0, 97, 8) #dte_str
o = substr($0, 198, 8) #dte_ver

if ( (f != spcs && f >= 19950401)
|| (o != spcs && o >= 19950401) ) #YYYYMMDD
{
print f o > "current.txt"
}
if if ( (f == spcs || f < 19950401)
&& (o == spcs || o < 19950401) ) #YYYYMMDD
{
print f o > "history.txt"
}
} END { }' DATA.txt

What is incorrect with this?
Thanks in advance.
Andre
Doug McClure

2004-03-26, 11:09 pm

Is there a typo here? You have "if if" after writing to current.txt.

It's difficult to propose a solution when we don't know what the
output is supposed to be. Is every line to be written to either
current.txt or history.txt? It looks to me like some lines are to be
written to neither. Is that what you want?

DKM



On 24 Mar 2004 13:03:43 -0800, norman_andre@hotmail.com (AJ Norman)
wrote:

>Hi,
>I have a datafile which contains two date columns.
>I'm using the following logic to create two new files base on these dates.
>It seems straight forward enough but I can't seem to get it to work.
>
>awk 'BEGIN{
>spcs=" "
>}
>{
>f = substr($0, 97, 8) #dte_str
>o = substr($0, 198, 8) #dte_ver
>
> if ( (f != spcs && f >= 19950401)
> || (o != spcs && o >= 19950401) ) #YYYYMMDD
> {
> print f o > "current.txt"
> }
> if if ( (f == spcs || f < 19950401)
> && (o == spcs || o < 19950401) ) #YYYYMMDD
> {
> print f o > "history.txt"
> }
>} END { }' DATA.txt
>
>What is incorrect with this?
>Thanks in advance.
>Andre



To contact me directly, send EMAIL to (single letters all)
DEE_KAY_EMM AT EarthLink.net. [For example X_X_X@EarthLink.net.]
Ed Morton

2004-03-26, 11:09 pm



AJ Norman wrote:
> Hi,
> I have a datafile which contains two date columns.
> I'm using the following logic to create two new files base on these dates.
> It seems straight forward enough but I can't seem to get it to work.
>
> awk 'BEGIN{
> spcs=" "
> }
> {
> f = substr($0, 97, 8) #dte_str
> o = substr($0, 198, 8) #dte_ver
>
> if ( (f != spcs && f >= 19950401)


I might be wrong, but when you compare a string "f" and a numeric
"19950401", I think that's treated as a string comparison rather than a
numeric one. To get a numeric comparison, try forcing f to a numeric value:

if ( (f != spcs && (1.0*f) >= 19950401)

<snip>
> } END { }' DATA.txt


Get rid of the "END { }" as it does nothing.

Ed.

AJ Norman

2004-03-26, 11:09 pm

Ed Morton <morton@lsupcaemnt.com> wrote in message news:<c3sva9$33q@netnews.proxy.lucent.com>...
> AJ Norman wrote:
>
> I might be wrong, but when you compare a string "f" and a numeric
> "19950401", I think that's treated as a string comparison rather than a
> numeric one. To get a numeric comparison, try forcing f to a numeric value:
>
> if ( (f != spcs && (1.0*f) >= 19950401)
>
> <snip>
>
> Get rid of the "END { }" as it does nothing.
>
> Ed.



That will do it.
Thanks Ed.
Sponsored Links







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

Copyright 2008 codecomments.com