bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] built-in variables in extensions


From: Andrew J. Schorr
Subject: Re: [bug-gawk] built-in variables in extensions
Date: Mon, 17 Dec 2012 16:34:38 -0500
User-agent: Mutt/1.5.21 (2010-09-15)

On Mon, Dec 17, 2012 at 04:16:15PM -0500, Assaf Gordon wrote:
> Ideally, to have a way to do the following (assuming the user knows there's a 
> column named "age"):
> 
>     gawk -l header '$age>43' INPUT > OUTPUT
> 
> That's all - concise and very clear.

Using the current git master tree, SYMTAB seems to do what you want.
However, based on Arnold's comments, I'm guessing this may not be supported
in the future:

bash-4.1$ cat /tmp/header.awk
NR == 1 {
        for (i = 1; i <= NF; i++)
                SYMTAB[$i] = i
        next
}

bash-4.1$ ( echo "name age" ; echo "Jim 10" ) | AWKPATH=/tmp ./gawk -i header 
'{print $age}'
10

This also seems to work at the moment:

bash-4.1$ cat /tmp/header.awk 
BEGIN {
        getline
        for (i = 1; i <= NF; i++)
                SYMTAB[$i] = i
}

But suppose this is not a stable feature.  Here is another approach:

bash-4.1$ cat /tmp/header.awk
@load "create_variable"

NR == 1 {
        for (i = 1; i <= NF; i++)
                create_variable($i, i)
        next
}

where "create_variable" is a shared library extension that exports the function
create_variable that creates a variable whose name is given in the first
argument and value in the second.  I believe it should be pretty trivial to
implement this extension using the current gawk api.  While neither an
include file nor a shared library can do this without help, the combination
should do it quite easily, unless I'm hallucinating.

Regards,
Andy



reply via email to

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