Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageIs 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.]
Post Follow-up to this message
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.
Post Follow-up to this messageEd Morton <morton@lsupcaemnt.com> wrote in message news:<c3sva9$33q@netnews.proxy.lucent.co
m>...
> 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.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.