Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Difference Between NF and $NF

450 views
Skip to first unread message

Buck Turgidson

unread,
Jan 26, 2005, 8:40:09 AM1/26/05
to
I am new to awk. Not sure I understand the different context in which NF is
dereferenced in the 2nd example. I would appreciate if someone could
explain it in a sentence or two.

# Print every input line with more than 4 fields:

NF > 4

# Print every input line in which the last field is more than 4:

$NF > 4


Kenny McCormack

unread,
Jan 26, 2005, 8:47:11 AM1/26/05
to
In article <r6jkc2-...@turf.turgidson.com>,

Looks like homework.

What's the difference between 4 and the 4th chicken (or the 4th anything) ?

Chris F.A. Johnson

unread,
Jan 26, 2005, 1:04:15 PM1/26/05
to

Run the scripts and see what the difference is.

Read the man page.

Think.

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
===================================================================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

Hrvoje Spoljar

unread,
Jan 26, 2005, 3:40:28 PM1/26/05
to
Buck Turgidson <jc...@hotmail.com> na grupi comp.lang.awk pise:

Ok maybe you can tell me what is the difference between

echo BLA

and

echo $BLA

--
____ __ ___| | ___ Ignorance is .~. hrvoje.spoljar@><.pbf.hr
(_-< '_ \/ _ \ |_/ -_) bliss, but / V \ irc # RoCkY
/__/ .__/\___/__/\___| knowledge is /( )\ icq : 53000945
|_| power! ^-^ http://spole.pbf.hr

Janis Papanagnou

unread,
Jan 26, 2005, 4:47:31 PM1/26/05
to
Buck Turgidson wrote:
> I am new to awk. Not sure I understand the different context in which NF is
> dereferenced in the 2nd example. I would appreciate if someone could
> explain it in a sentence or two.

Not quite sure what your problem is, but I suppose it's the "dereferencing"
concept.

> # Print every input line with more than 4 fields:
>
> NF > 4

The variable name NF is dereferenced as in most other programming languages.
The value of the (built-in) variable NF is compared against the value 4.

> # Print every input line in which the last field is more than 4:
>
> $NF > 4

You can access the 1st, 2nd, 3rd,... input field by $1 $2 $3 ...
Think of the numbers 1, 2, 3, ... as something like built-in variables.

The $ is the operator to access the fields. You can even use spaces: $ NF
Don't confuse the $ (access fields) in awk with $ (dereference) in shells.

If you specify a name after the $ operator it is considered to be a variable
and dereferenced to obtain the value stored in the variable, then the value
is used to access the field. NF is a built-in variable that carries always
a number. But you may as well use own variables: BEGIN { x = 5 } $x > 0

NF > 4 # deref(NF) > 4
$ NF > 4 # deref(fieldWithNr(deref(NF))) > 4

As in other languages you can ignore the implicit deref operations and focus
on the essential

NF > 4 # NF > 4
$ NF > 4 # fieldWithNr(NF) > 4

NF carries the number of fields, thus may be used to address the last field.

Janis

Bill Marcum

unread,
Jan 30, 2005, 9:49:22 PM1/30/05
to
On Wed, 26 Jan 2005 20:40:28 +0000 (UTC), Hrvoje Spoljar
<fc...@k.cos.ue> wrote:
>
> Ok maybe you can tell me what is the difference between
>
> echo BLA
>
> and
>
> echo $BLA
>
"echo" is a shell command, not an awk command. Try this:
BLA=4
echo BLA
echo $BLA

Now for some real fun:
BLA=PATH
eval echo \$$BLA

Hrvoje Spoljar

unread,
Jan 31, 2005, 11:18:37 AM1/31/05
to
Bill Marcum <bma...@iglou.com.urgent> na grupi comp.lang.awk pise:

So ? guy asked what is the difference betwen NF and $NF
so i thaugth maybe he is familiar with variables and strings
and example i gave would clear up the things... i never said echo was
awk command :-)

0 new messages