FS odd behavior? 
Author Message
 FS odd behavior?

hi folks-

i have an awk problem that must be my doing.
basically, i set the FS variable to a comma
and then test for the second field to equal
the string "PRCP" but with bizarre results
( to me ).

in the first 'if' statement below, the test returns
true as if FS was still a space or tab?

in the second 'if' statement, the /test/ returns true,
but the fourth field is /printed out/ correctly.
that is, the -if- test is on one interpretation
of FS and the following -print- statement is of
a different interpretation.

pls. enlighten me.  or flame me.  just so i can
get some sleep.

thx.

the code run on a Solaris box from the prompt as in:
%./foo.awk testfile

----- % cut here % -----
#! /usr/xpg4/bin/awk -f
#
#
BEGIN { FS=OFS="," }
{
  if ( $4 == PRCP )
    print $1, $2, $3, $4

  if ( $4 == PRCP )
    print "field 4 = " $4

Quote:
}

----- % cut here % -----

the file "testfile" consisting of three long lines
of 34 fields ( comma-delimited fields ).
----- % cut here % -----
CORCORAN IRRIG
DIST,PRCP,199001,20,0,0,0,0,0,0,0,0,0,0,42,30,4,0,24,0,0,0,0,0,0,
0,0,0,0,0,0,0,9,6
CORCORAN IRRIG
DIST,PRCP,199002,0,0,48,0,0,8,0,0,0,0,0,0,0,0,0,14,8,0,0,0,0,0,0,
0,0,0,0,0,-99999,-99999,-99999
CORCORAN IRRIG
DIST,PRCP,199003,0,7,0,6,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0
----- % cut here % -----



Sun, 14 Dec 2003 03:05:55 GMT  
 FS odd behavior?

Quote:

> i have an awk problem that must be my doing.

It is.

Quote:
> basically, i set the FS variable to a comma
> and then test for the second field ...

Second or fourth?

Quote:
> ... to equal the string "PRCP" but with bizarre
> results (to me).

You're comparing the value of $4 with the empty string in the
uninitialized variable named PRCP and getting the usual mundane
results (to me). The only excitment occurs when, sometimes, the
value of $4 is 0, so the comparison '$4 == PRCP' is true!

Put double quotes around "PRCP" and use $2, not $4.

Quote:
> in the first 'if' statement below, the test returns
> true as if FS was still a space or tab?

> in the second 'if' statement, the /test/ returns true,
> but the fourth field is /printed out/ correctly.
> that is, the -if- test is on one interpretation
> of FS and the following -print- statement is of
> a different interpretation.

Huh?! Your problem has nothing to do with the value of FS.

Quote:
> pls. enlighten me.  or flame me.  just so i can
> get some sleep.

> the code run on a Solaris box from the prompt as in:
> %./foo.awk testfile

> ----- % cut here % -----
> #! /usr/xpg4/bin/awk -f
> #
> #
> BEGIN { FS=OFS="," }
> {
>   if ( $4 == PRCP )
>     print $1, $2, $3, $4

>   if ( $4 == PRCP )
>     print "field 4 = " $4
> }
> ----- % cut here % -----

> the file "testfile" consisting of three long lines
> of 34 fields ( comma-delimited fields ).
> ----- % cut here % -----
> CORCORAN IRRIG
> DIST,PRCP,199001,20,0,0,0,0,0,0,0,0,0,0,42,30,4,0,24,0,0,0,0,0,0,
> 0,0,0,0,0,0,0,9,6
> CORCORAN IRRIG
> DIST,PRCP,199002,0,0,48,0,0,8,0,0,0,0,0,0,0,0,0,14,8,0,0,0,0,0,0,
> 0,0,0,0,0,-99999,-99999,-99999
> CORCORAN IRRIG
> DIST,PRCP,199003,0,7,0,6,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,
> 0,0,0,0,0,0,0
> ----- % cut here % -----

--
Jim Monty

Tempe, Arizona USA


Sun, 14 Dec 2003 03:28:50 GMT  
 FS odd behavior?
Could it have been any more obvious?  Doh!

Thanks.



Sun, 14 Dec 2003 03:43:25 GMT  
 FS odd behavior?

Quote:

> Could it have been any more obvious?

Sure:

    $ perl -Mstrict -F, -wane '$F[1] eq PRCP' /dev/null
    Bareword "PRCP" not allowed while "strict subs" in use at -e line 1.
    Execution of -e aborted due to compilation errors.
    $

Compile-time strictures are more obvious. Hell, they're show stoppers!

    $ gawk --lint -F, '$4 == PRCP' /dev/null
    $

--
Jim Monty

Tempe, Arizona USA



Sun, 14 Dec 2003 06:19:40 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Odd behavior

2. Odd REAL Database behavior

3. DropObject odd behavior

4. c5ABC - odd (to me) behavior adding contents of browse to txt file

5. Odd SQL behavior?

6. Odd behavior from Generated Step Locator on Browse

7. Odd behavior when showing and hiding grouped controls and indicators

8. Odd Hash Behavior (long)

9. Odd behavior question

10. Odd behavior of Open Watcom

11. Odd mail(...) behavior under Win2K/Apache/PHP

12. Odd String Comparison Behavior Question

 

 
Powered by phpBB® Forum Software