Help with awk if/else-statement
I'm trying to write an awk script to process some text file based on the number of fields in each line. That is, if a line contains, for instance, 10 words, I'd like to process it differently than if it has 11 words. However, I don't seem able to get the if/else-statements correct.
This is the script:
#!/bin/awk
{
if ( NF = 10 )
print "10," NF "," $0
else
print "xx," NF "," $0
}
I'd expect that the script will output something like this:
10,10,line with 10 words
xx,13,line with 13 words
Instead I'm getting:
10,10,line with 10 words
10,10,line with 13 words
I changed the script to
{
if ( NF = 500 ) # of course, there are no lines with 500 fields!
print "500," NF "," $0
else
print "xx," NF "," $0
}
and now I'm getting this:
500,500,line with 10 words
500,500,line with 13 words.
It seems that instead of comparing "NF" to the number 10 (or 500), the script is SETTING the variable NF to 10 (or 500).
Why????
TIA, Tina
G5 Dual 1.8 (and lots of others, @ work and @ home), Mac OS X (10.5.6), iPhone 3G