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: Assaf Gordon
Subject: Re: [bug-gawk] built-in variables in extensions
Date: Mon, 17 Dec 2012 16:16:15 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.4) Gecko/20120510 Icedove/10.0.4

Hello Andrew,

Andrew J. Schorr wrote, On 12/17/2012 03:58 PM:
> 
> On Mon, Dec 17, 2012 at 03:45:23PM -0500, Assaf Gordon wrote:
>> Continuing my quest for header-line support in gawk...
>> Perhaps it's too complicated without internal code changes - in which case 
>> I'll drop it. WDYT?
> 
> I'm curious -- what are you trying to achieve?  Many of my scripts
> start like this:
> 
> # parse header record: map column names to field numbers
> NR == 1 {
>    for (i = 1; i <= NF; i++)
>       m[$i] = i
>    next
> }
> 
> I can then access fields by column title, e.g. $m["name"]
> 

I'm trying to achieve exactly the same thing, with fewer keystrokes :)
But more seriously, I want a reusable module (library/extension) that provides 
the same functionality,
and requires no initialization and minimal 'overhead' - both for my benefit, 
and for few fellow users who are not AWK experts.

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.

I'm well aware this just a bit more lazy than to check the input file and see 
that the "age" column is the third column, and do

    gawk 'NR==1 { print } NR>1 && $3>43' INPUT > OUTPUT

But it's very useful and I found myself many times wishing for such a 
quick-and-dirty filter.

If it requires more overhead (like your initialization code, or remembering to 
use "$m["name"]") - then it's not really worth it, I'll just revert back to 
"$3" .

Such functionality is useful enough for other people to write such modified awk 
version, but sadly they didn't based it on "gawk" [1] .


-gordon


[1] 
https://github.com/lh3/bioawk/commit/12cd2bba3b823b3084f5e18b85e456df1000849e




reply via email to

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