difficulty with awk

hello folks,

       i am stuck with this awk command. i need to calculate the sum of a column of values on a flatfile and i am using the following command :
awk -F"|" '{x += $10} END {print "Sum: "x}' standard_csv_file1.out

that flatfile contains 180 fields and i am getting the following error.

awk: record `"D"||||||2449|||203|...' has too many fields
 record number 1

i tried the same command when i had few fields (may be 30 or so) it worked fine. but in order to standardise the flatfile, i have to add too many null value fields and now the total number of fields is 180 and the command doesn't work saying too many fields. any way around please. :confused:

Hi jdsony,

Try if next command works for you:

$ cut -d"|" -f10 standard_csv_file1.out | awk '{ x += $1 } END { print "Sum: " x }'

Regards,
Birei

What's your OS? You may get results with nawk or gawk (gawk doesn't have a field limit, iirc).

thanks for the responses guys. the cut command doesn't work as well unfortunately. i am working on solaris. it seems gawk doesn't work on it but nawk works on it. could you please tell me the equivalant nawk command for my command. i am a novice with poor unix skills. please don't mind.

It should be the same syntax, just nawk instead of awk .

Hi Birei, thank you.

$ cut -d"|" -f10 standard_csv_file1.out | awk '{ x += $1 } END { print "Sum: " x }'

it works mate. CarloM, thanks nawk too works. thank you both.