loop + sum + print using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting loop + sum + print using awk
# 1  
Old 07-21-2010
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
Code:
abc.txt
100,200,300,400,500,600,700,800,900
101,201,301,401,501,601,701,801,901



output :
Code:
201,100
401,100
601,100

-- and so on.

Code:
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

Last edited by Scott; 07-21-2010 at 05:10 AM.. Reason: Code tags, PLEASE!
# 2  
Old 07-21-2010
Hi.

You are using a shell variable inside awk.

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

Code:
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:
Code:
awk -F"," '{
    for( i = 1; i <= NF; i++ ) x[i] += $i
  }

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

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


Last edited by Scott; 07-21-2010 at 06:02 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - Print Sum

Hi, I have an awk command that I am using, and part of it sums COL_9 however when I read the output it is not including decimal places; awk ' BEGIN{FS=OFS=","} NR==1{print;next} {a+=$9 c = $12 d = $18 } END{for(i in a) {split(i,b,";"); print $1, $2, $3, b, $5, $6, b, b, a, $10, $11,... (1 Reply)
Discussion started by: Ads89
1 Replies

2. Shell Programming and Scripting

Print awk output in same line ,For loop

My code is something like below. #/bin/bash for i in `ps -ef | grep pmon | grep -v bash | grep -v grep | grep -v perl | grep -v asm | grep -v MGMT|awk '{print $1" "$8}'` do echo $i ORACLE_SID=`echo $line | awk '{print $2}'` USERNAME=`echo $line | awk '{print $1}'` done ============= But... (3 Replies)
Discussion started by: tapia
3 Replies

3. Shell Programming and Scripting

awk sum of all columns needs to print exact amount

Hi I have attached txt file as input, and i'm able to calculate sum of columns at the end but the format of sum is not coming up right. awk -F"," '{for (i=4;i<=NF;i++) sum+=$i}{print}; END { sum="Total:"; for (i=1;i<=NF;i++) {printf sum ","} print "\n"}' input.txt check the o/p file, at... (6 Replies)
Discussion started by: manas_ranjan
6 Replies

4. Post Here to Contact Site Administrators and Moderators

awk to sum in Loop

i want code in awk with loop to get the sum * is delimiter in file TOTAL_AMOUNT=SUM(CLP04) suppose there are 12 CLP04 segment in my file i want to add upto 5 CLP04 then print next line after BPR segment after calculate the total amount CLP04 means ex ... (5 Replies)
Discussion started by: MOHANP12
5 Replies

5. Shell Programming and Scripting

Search two patterns using awk to print the variable sum

Coins.txt: gold 1 1986 USA American Eagle gold 1 1908 Austria-Hungary Franz Josef 100 Korona silver 10 1981 USA ingot gold 1 1984 Switzerland ingot gold 1 1979 RSA Krugerrand gold 0.5 1981 RSA Krugerrand gold 0.1 1986 PRC Panda silver 1 1986 USA Liberty dollar gold 0.25 1986 USA Liberty... (2 Replies)
Discussion started by: Ramesh M
2 Replies

6. Shell Programming and Scripting

For loop and awk print

Hi, i am using a variable tmpVar, using variable data i need implement for loop tmpVar="abc bbc cbc nbc mbc" # valiable having total number of words= 5 so i need to loop 5 times. tmpVar="abc bbc cbc nbc mbc" tmpcnt=`echo $tmpVar|wc -w` for cnt in 1..$tmpcnt do t1=`echo... (4 Replies)
Discussion started by: onesuri
4 Replies

7. Shell Programming and Scripting

awk to loop lines and print substring

The input data: mbeanName: com.sap.default:name=tc~bl~deploy_controller,j2eeType=SAP_J2EEServicePerNode,SAP_J2EEClusterNode=3620850,SAP_J2EECluster=XXX Get attribute Properties: {undepl_parallelism_strategy=normal, deployment_forbidden=off, locking_retries=50, suppress_ds_warnings=on,... (5 Replies)
Discussion started by: ux4me
5 Replies

8. Shell Programming and Scripting

sum from ls -al |awk '{print $5}'

// AIX 5.3 & AIX 6.1 ls -al |awk '{print $5}' This gives each file's size in byte. I need to get: - the sum of all files in Giga bytes with loop. - excluding the size of directories (ls -al returns the size of directories). There are hundreds and thousands of files, so summing up... (8 Replies)
Discussion started by: Daniel Gate
8 Replies

9. Shell Programming and Scripting

Print sum and relative value of the sum

Hi i data looks like this: student 1 Subject1 45 55 Subject2 44 55 Subject3 33 44 // student 2 Subject1 45 55 Subject2 44 55 Subject3 33 44 i would like to sum $2, $3 (marks) and divide each entry in $2 and $3 with their respective sums and print for each student as $4 and... (2 Replies)
Discussion started by: saint2006
2 Replies
Login or Register to Ask a Question