Sum with condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum with condition
# 1  
Old 04-11-2013
Sum with condition

Hi all,
I have question on how to sum up a column with "condition" for following listing example
Code:
imvprd_lun01  34                     imvprd_pool01 admin  19    
imv1prd_lun01 188   IMBDW_N0 imvprd_pool01 admin  133   
imv1prd_lun02 1236  IMBDW_N1 imvprd_pool01 admin  1125  
imv1prd_lun03 1236  IMBDW_N2 imvprd_pool01 admin  1129  
imv1prd_lun04 1236  IMBDW_N3 imvprd_pool01 admin  1133  
imv2prd_lun01 1236  IMBDW_N4 imvprd_pool01 admin  1132  
imv2prd_lun02 1236  IMBDW_N5 imvprd_pool01 admin  1130  
imv2prd_lun03 1236  IMBDW_N6 imvprd_pool01 admin  973   
imv2prd_lun04 1236  IMBDW_N7 imvprd_pool01 admin  963   
imvha_lun01   17                     imvprd_pool01 admin  0

You can see that there are 2 odds line without the string in 3rd column.
My purpose is how can I sum up column 6th.
how can I check $5 if digit sum it up, else I want to sum it up using $6 ?

Thanks.

Last edited by Franklin52; 04-11-2013 at 04:07 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 04-11-2013
Use $NF

Code:
awk '{sum+=$NF}END{print sum}' infile

--ahamed
# 3  
Old 04-11-2013
thanks ahmed for your fast response and answer u given is a good move.
Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns satisfy the condition

HI All, I'm embedding SQL query in Script which gives following output: Assignee Group Total ABC Group1 17 PQR Group2 5 PQR Group3 6 XYZ Group1 10 XYZ Group3 5 I have saved the above output in a file. How do i sum up the contents of this output so as to get following output: ... (4 Replies)
Discussion started by: Khushbu
4 Replies

2. UNIX for Dummies Questions & Answers

Sum with condition

Dear masters, I have file input 011171646073|12129|12129|A027|20141001|20141015|2014|10|01|2013|10|15 011171646076|12129|12129|A027|20141001|20141012|2014|10|01|2014|09|12 011171646078|12129|12129|A027|20141001|20141015|2014|10|01|2014|10|15... (6 Replies)
Discussion started by: radius
6 Replies

3. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Shell Programming and Scripting

Sum Of Column Based On Column Condition

I have a following inputfile MT,AP,CDM,TTML,MUM,GS,SUCC,3 MT,AP,CDM,TTSL,AP,GS,FAIL,9 MT,AP,CDM,RCom,MAH,GS,SUCC,3 MT,AP,CDM,RTL,HP,GS,SUCC,1 MT,AP,CDM,Uni,UPE,GS,SUCC,2 MT,AP,CDM,Uni,MUM,GS,SUCC,2 TTSL,AP,GS,MT,MAH,CDM,SUCC,20 TTML,AP,GS,MT,MAH,CDM,FAIL,10... (2 Replies)
Discussion started by: siramitsharma
2 Replies

5. Shell Programming and Scripting

How to sum these value.

Hi, Unix Gurus, I need sum values from a file. file format like: 0004004 0000817 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0004406 the result should be 459227 (817+45000 ... + 4406) anybody can help me out (7 Replies)
Discussion started by: ken002
7 Replies

6. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

7. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

8. 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

9. UNIX for Dummies Questions & Answers

sum of column with condition

1 IT 50 2 IT 40 3 Finance 200 4 MP 30 5 MP 10 6 HQ 30 how to use awk to make it display it like this IT 90 MP 40 HQ 30 Finance 200 (1 Reply)
Discussion started by: minwei86
1 Replies

10. Shell Programming and Scripting

sum

Hello everyone I need to write a script that sums numbers passed to it as arguments on the command line and displays the results. I must use a for loop and then rewrite it using a while loop. It would have to output something like 10+20+30=60 this is what I have so far fafountain@hfc:~$ vi sum... (1 Reply)
Discussion started by: Blinky85
1 Replies
Login or Register to Ask a Question