Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Meaning of NF{ a[$1]=a[$1]" "$2 ; next }?

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-25-2010
npatwardhan npatwardhan is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 23 April 2010, 6:28 PM EDT
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
Meaning of NF{ a[$1]=a[$1]" "$2 ; next }?

Hi Guys,

I have the following statement from a bigger script. Can someone please explain what it does?


Code:
 
NF{ a[$1]=a[$1]" "$2 ; next }
END{for (i in a) {print i,a[i] } }

Thanks.
Sponsored Links
    #2  
Old Unix and Linux 01-25-2010
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 4 July 2015, 5:46 AM EDT
Location: Zürich
Posts: 8,570
Thanks: 337
Thanked 1,080 Times in 938 Posts
Hi.

It's part of an awk script.

NF{...} means if there are more than zero fields in the current record (i.e. not a blank line) perform the action between { and }

An awk script is made up of patterns (conditions, really) and actions

PATTERN { ACTION }

If PATTERN evaluates to true, then ACTION is performed.

[NF is an awk variable giving the number of fields in the current record. If it's non-zero, ACTION is performed.]

{ a[$1]=a[$1]" " $2; next } means append the value of field two ($2) to an array (a) indexed by the value of field one ($1).

The END section is performed after all input files are processed.

for(i in a) means assign to i, in turn, each element of a
and print the index value i, followed by the value referenced by that index (a[i]).

The two lines you posted say: add field two to an array called a, indexed by distinct values of field one.

HTH Linux
Sponsored Links
    #3  
Old Unix and Linux 01-25-2010
npatwardhan npatwardhan is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 23 April 2010, 6:28 PM EDT
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the explanation. That helps a lot.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Meaning of "> /dev/null 2>&1" salanalani Shell Programming and Scripting 10 07-22-2008 03:29 PM
What's the meaning of "Dj$w.$m" in /etc/mail/sendmail.cf rainbow_bean AIX 1 01-30-2008 04:03 PM
What's meaning of "POWER" in diag command.? pattarapongn AIX 1 09-24-2006 06:57 PM
What is the meaning of "tab" in script ? so_friendly Shell Programming and Scripting 3 07-10-2006 01:34 AM
help!what`s the meaning of the "deltas" beijingzhzj UNIX for Dummies Questions & Answers 3 04-01-2004 12:29 PM



All times are GMT -4. The time now is 04:55 AM.