Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie question about number fields

Status
Not open for further replies.

mobidyc

Technical User
Sep 28, 2006
2
FR
Hello,

my english seems to be bad (i'm french), so i don't understand the $NR var.
NR: The number of fields in the current record

echo "test toto tata" |awk '{print "number of fields: "$NR}'
number of fields: test

there is 3 fields, the results would be '3', no ?

please, help me to understand.

 
Hi

There is something wrong :
[ul]
[li][tt]NR[/tt] - number of records[/li]
[li][tt]NF[/tt] - number of fields[/li]
[/ul]
And those begining with dolar sign ( $ ) are field variables. So [tt]$NR[/tt] is the [tt]NR[/tt]th field of the current record. And while that was the first record, your expression evaluates to [tt]$1[/tt], so the first field was [tt]print[/tt]ed.

You want this :
Code:
echo "test toto tata" |awk '{print "number of fields: "NF}'

Feherke.
 
NR (without $) = Record Number
NF (without $) = Number of Fields in the current record
$NF (with $) = Value of last field in the current record

Anyway: man awk

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
many thanks for your answers and sorry for my mistake.

a second thanks for the quick answers ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top