bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: feature: expose POSITION of parsed columns (fields) as a variable/fu


From: Andrew J. Schorr
Subject: Re: feature: expose POSITION of parsed columns (fields) as a variable/function?
Date: Thu, 13 May 2021 10:17:53 -0400
User-agent: Mutt/1.5.21 (2010-09-15)

On Thu, May 13, 2021 at 06:54:52AM -0700, david kerns wrote:
> You're thinking about awk internals incorrectly... consider the following:
> 
> echo "    1     2 3" | awk '{
> s=$0; # first save the input record, by now awk internals has already parsed
>       # the record into numbered fields losing any repeated white space
> delimiter,
>       # unless FS was previously explicitly set
> print $0; # show the record as is
> $2="z"; # ANYTIME $<number> appears on the LHS of an equals,
>         # awk internally rebuilds $0 using OFS
> print $0; # show $0 reconstructed with OFS
> $0=s; # restore $0, also causes awk to internally reparse the record
>       # into numbered fields, again, using FS
> print $0; # show restored line
> }'
>     1     2 3
> 1 z 3
>     1     2 3
> 
> $2 is not a pointer into the record $0, it is a stand alone object.

Actually, internally, for n > 0, $n does contain a pointer into the $0 record
buffer, although that's an implementation detail that's hidden from the user.

I haven't studied it closely, but I think it could be possible without too much
difficulty to add an FPOS array as suggested, or enhance the split function to
take an additional argument that would return an array containing field
positions. Enhancing split() would probably be easier. Whether adding such a
feature would be wise is a different question.  That being said, I have also
encountered situations where it would be useful to have access to the field
positions.

Regards,
Andy



reply via email to

[Prev in Thread] Current Thread [Next in Thread]