NF and shell variables 
Author Message
 NF and shell variables

Hello all,

I would like to compare NF to a shell variable named "fields" containing the
desired number of fields per line;

awk '{if (NF == fields) print}'

However, I have been unable to find any documentation that would suggest that
this simple (yet powerful) comparison is supported by awk. Is my fear correct?

Thanks,

Bill Barnett
"I intend to live forever. So far, so good!"



Sat, 31 Jan 2004 09:52:04 GMT  
 NF and shell variables


Quote:
>Hello all,

>I would like to compare NF to a shell variable named "fields" containing the
>desired number of fields per line;

>awk '{if (NF == fields) print}'

NF=3
awk "NF == $NF"


Sat, 31 Jan 2004 10:04:30 GMT  
 NF and shell variables


Quote:
>Hello all,

>I would like to compare NF to a shell variable named "fields" containing the
>desired number of fields per line;

>awk '{if (NF == fields) print}'

>However, I have been unable to find any documentation that would suggest that
>this simple (yet powerful) comparison is supported by awk. Is my fear correct?

>Thanks,

>Bill Barnett
>"I intend to live forever. So far, so good!"

 awk '{if (NF == fields) print}' fields=$elephant

 or, if the variable is required in BEGIN,

 gawk -v fields=$elephant '{if (NF == fields) print}'

 The gawk instruction summary "card", which is distributed with the source
 code has this kind of stuff on it.

Erik

Erik
--
 ----
 The human mind ordinarily operates at only ten percent of its capacity
 -- the rest is overhead for the operating system.



Sat, 31 Jan 2004 10:10:08 GMT  
 NF and shell variables

BB Hello all,
BB
BB I would like to compare NF to a shell variable named "fields" containing the
BB desired number of fields per line;
BB
BB awk '{if (NF == fields) print}'
BB
BB However, I have been unable to find any documentation that would suggest that
BB this simple (yet powerful) comparison is supported by awk. Is my fear correct?
BB
BB Thanks,
BB
BB Bill Barnett
BB "I intend to live forever. So far, so good!"

It never fails! As soon as I posted my question I found my answer;

http://www.ee.byu.edu/unix-faq/subsection3_5_12.html

Thanks to those who took the time to read my question and special thanks to
those who responded.

Bill Barnett
"Test results are inconclusive. He's either a very stupid human,
or a very clever ape."



Sat, 31 Jan 2004 10:55:16 GMT  
 NF and shell variables

Quote:

> > I would like to compare NF to a shell variable named "fields" containing
> > the desired number of fields per line;

> > awk '{if (NF == fields) print}'

> > However, I have been unable to find any documentation that would suggest
> > that this simple (yet powerful) comparison is supported by awk. Is my fear
> > correct?

> It never fails! As soon as I posted my question I found my answer;

> http://www.ee.byu.edu/unix-faq/subsection3_5_12.html

This page is old (exactly seven years--it's dated August 14, 1994)
and doesn't represent the state of the art. There's no mention of
the now-common -v option, and it describes the foo=bar method as
"an often undocumented feature." (I've read a lot of awk documentation
and I've never noticed it missing.)

Your question is the awk MFAQ (Most Frequently Asked Question),
and the MFOR (Most Frequently Offered Response) is not the MFA
(Most Favorable Answer), IMHO. Perhaps the Unix FAQ is the culprit
here. I just don't see how anyone could prefer ugly shell quoting
tricks to the simple and more flexible technique that Messrs. A.,
W., and K. built into the language. It's almost never mentioned in
this newsgroup, but the ability to initialize the value of a variable
differently for multiple input files is actually a quite powerful
feature. (See TAPL for examples.) I also see the -v option too often
recommended where it's not strictly needed. Its purpose is to
initialize a global variable that is accessible with a BEGIN rule.

Infer your own dogma: these are just my opinions.

--
Jim Monty

Tempe, Arizona USA



Sun, 01 Feb 2004 00:04:56 GMT  
 NF and shell variables
Could you tell us more about TAPL?
thanks
Quote:
> W., and K. built into the language. It's almost never mentioned in
> this newsgroup, but the ability to initialize the value of a variable
> differently for multiple input files is actually a quite powerful
> feature. (See TAPL for examples.) I also see the -v option too often
> recommended where it's not strictly needed. Its purpose is to
> initialize a global variable that is accessible with a BEGIN rule.

> Infer your own dogma: these are just my opinions.

> --
> Jim Monty

> Tempe, Arizona USA



Mon, 09 Feb 2004 05:24:32 GMT  
 NF and shell variables

Quote:

>Could you tell us more about TAPL?
>thanks

>> W., and K. built into the language. It's almost never mentioned in
>> this newsgroup, but the ability to initialize the value of a variable
>> differently for multiple input files is actually a quite powerful
>> feature. (See TAPL for examples.) I also see the -v option too often
>> recommended where it's not strictly needed. Its purpose is to
>> initialize a global variable that is accessible with a BEGIN rule.

>> Infer your own dogma: these are just my opinions.

>> --
>> Jim Monty

>> Tempe, Arizona USA

Bernard,

   Jim appears to be referring to "The AWK Programming Language",
   published by Addison-Wesley, authored by Alfred Aho, Brian Kernighan, and
   Peter Weinberger. (I find it delightfully concise. Caution: It does not cover
   all the cute gawk enhancements, and does not mention toupper() or tolower(),
   which most awks and nawks seem to have)

   Page 63 refers to the simple expedient of interspersing var=text expressions
   with filenames in the argument list. Naturally this can be used to redefine a
   variable between files, if desired.

Erik

--
 ----
 The human mind ordinarily operates at only ten percent of its capacity
 -- the rest is overhead for the operating system.



Mon, 09 Feb 2004 16:24:20 GMT  
 NF and shell variables
The "TAPL" of course!
Shame on me for ignoring the "ancien testament" ;o)
and thanks Erik for helping even those who are at about 100%'s capacity
clogged by their Operating System...

Quote:

> Bernard,

>    Jim appears to be referring to "The AWK Programming Language",
>    published by Addison-Wesley, authored by Alfred Aho, Brian Kernighan,
and
>    Peter Weinberger. (I find it delightfully concise. Caution: It does not
cover
>    all the cute gawk enhancements, and does not mention toupper() or
tolower(),
>    which most awks and nawks seem to have)

>    Page 63 refers to the simple expedient of interspersing var=text
expressions
>    with filenames in the argument list. Naturally this can be used to
redefine a
>    variable between files, if desired.

> Erik

> --
>  ----
>  The human mind ordinarily operates at only ten percent of its capacity
>  -- the rest is overhead for the operating system.



Fri, 13 Feb 2004 02:06:53 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. awk variables --> shell variables

2. AWK-Script to set a shell variable (Win32) ?

3. Passing shell variables to nawk snipet

4. Passing command line variables to AWK in shell script

5. setting shell variables with results from awk

6. shell variable

7. Exporting awk variables to the UNIX shell

8. help for newbie: way to get the value of an awk variable in a shell script

9. how to use a shell variable in awk for pattern matching

10. Shell variables in AWK

11. Quoting Swamp: Saving an awk command in a shell variable

12. print shell variable

 

 
Powered by phpBB® Forum Software