🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by winkler@eas.iis. Mon, 12 May 1997 00:00
FS Behaviour differs between awk and gawk
#3902
Author: winkler@eas.iis.
Date: Mon, 12 May 1997 00:00
27 lines
400 bytes
To all awk-gurus:

the folloging lines produces with awk and gawk different output:


echo "F1: F2 F2_1 : F3" | awk '{ FS=":"; print $2}'

with awk:
" F2 F2_1 "

with gawk:
"F2"

When I use gawk, the following works fine:
echo "F1: F2 F2_1 : F3" | gawk 'BEGIN {FS=":"} { print $2}'

--> the same output as awk above.

Is this okay or a bug in awk or/and gawk or somethere else ( ;-) ).

Frank.





Re: FS Behaviour differs between awk and gawk
#3912
Author: churchyh@ccwf.cc
Date: Mon, 12 May 1997 00:00
31 lines
994 bytes
In article <5l770m$b1l$1@news.fhg.de>,
Frank Winkler <winkler@eas.iis.fhg.de> wrote:

> To all awk-gurus:
> the following lines produce with awk and gawk different output:

> echo "F1: F2 F2_1 : F3" | awk '{ FS=":"; print $2}'

> with awk: " F2 F2_1 "
> with gawk: "F2"

> When I use gawk, the following works fine:

> echo "F1: F2 F2_1 : F3" | gawk 'BEGIN {FS=":"} { print $2}'
> --> the same output as awk above.

I don't know whether or not the " F2 F2_1 " response is officially
defined as the "correct" behavior, but notice that in the first code
snippet you're (conceptually) redefining FS _after_ g?awk has already
read in the line, so that it's not entirely surprising that there may
be implementation differences.

What version of Gawk are you using?  Gawk 2.15.6 installed here
returns " F2 F2_1 " on the first snippet.

--
"His name is Henry, a proof how unequally the gifts of fortune are bestowed."
-Jane Austen, 10/14/1813 || Henry Churchyard http://uts.cc.utexas.edu/~churchh


Re: FS Behaviour differs between awk and gawk
#3917
Author: Jacob Salomon
Date: Tue, 13 May 1997 00:00
67 lines
2074 bytes
Frank Winkler wrote:

> To all awk-gurus:

I'm no guru but I think I understand what happened in the gawk example.

> the folloging lines produces with awk and gawk different output:
>
> echo "F1: F2 F2_1 : F3" | awk '{ FS=":"; print $2}'
>
> with awk:
> " F2 F2_1 "
>
> with gawk:
> "F2"
>
> When I use gawk, the following works fine:
> echo "F1: F2 F2_1 : F3" | gawk 'BEGIN {FS=":"} { print $2}'
>
> --> the same output as awk above.

Sounds like questionable behavior in your implentation of awk.  I just
tried your example (without the BEGIN) on my HP and got the same result,
but it's still questionable.

As I understand it, by the time it executes the action:
   '{ FS=":"; print $2}'
it has (or should have) already read the input line and split it
according to the default FS (blank).  Setting FS to ":" should not have
an influence this line anymore, though if you had piped 2 identical
lines in there it would have made sense to produce that < F2 F2_1 > for
the second line.

It would appear that your (and my) implentation of awk splits the line
according to the latest value of FS every time you want to use a
component.  To test this theory, I ran the following:

echo "F1: F2 F2_1-F2_2: F3" |
awk '
{ FS=":"; print $2
  FS="-"; print $2
}
'
 F2 F2_1-F2_2
F2_2: F3

I am fairly certain that gawk would have produced:
F2
F2

> Is this okay or a bug in awk or/and gawk or somethere else ( ;-) ).

It's arguable which is better.  For what my opinion is worth, I happen
to agree with the gawk approach; I see it as cleaner and more efficient.
The added flexibility of the awk approach is, IMO, not worth the sloppy
practice it encourages.  However, you hit the spot when you put the FS
setting in the BEGIN execution.  That's where it belongs.
--
    -- Jake (In pursuit of undomesticated aquatic avians)

+-----------------------------------------------------------+
|    Impeccable Logic: A thought process which successfully |
|                      resists chicken bites                |
+-----------------------------------------------------------+


Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads