loop + sum + print using awk

Hi,

I am unable sum of each column in the loop usng awk command.

Awk is not allowing the parameters in the command.

i am facing the below error.

awk: 0602-562 Field $() is not correct.

Source file

abc.txt
100,200,300,400,500,600,700,800,900
101,201,301,401,501,601,701,801,901

output :

201,100
401,100
601,100

-- and so on.

outputfile="/temp/out.txt"
a=100
for i in 1 2 3 4 5
do
  Actual=`awk -F"," '{x += $i} END {print x}' Trailer_Data_Details.csv`
  echo $Actual $a > $outputfile
done

Thanks

Hi.

You are using a shell variable inside awk.

If you make it an awk variable, it should just work.

outputfile="/temp/out.txt"
a=100
for i in 1 2 3 4 5
do
  Actual=`awk -F"," '{x += $i} END {print x}' i=$i Trailer_Data_Details.csv`
  echo $Actual,$a > $outputfile
done


# Your for-loop only takes the first 5 columns...
201,100
401,100
601,100
801,100
1001,100

Doing the whole thing just in awk, for every column:

awk -F"," '{
    for( i = 1; i <= NF; i++ ) x += $i
  }

  END {
    for( i = 1; i <= NF; i++ ) print x "," a 
  }' a=100 

201,100
401,100
601,100
801,100
1001,100
1201,100
1401,100
1601,100
1801,100