For Loop & SUM


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For Loop & SUM
# 1  
Old 02-03-2013
For Loop & SUM

HTML Code:
pcmpath query device |awk 'BEGIN{print "DEVICE NAME\tSERIAL"} /DEVICE NAME/ {printf "%s\t", $5; getline; print substr($2, length($2)-3)}'
This script returns output like below to pull out "DEVICE NAME SERIAL".

HTML Code:
......
hdisk28 110B
hdisk29 1112
hdisk30 1115
hdisk31 1116
hdisk32 1128
hdisk33 112D
hdisk34 112E
hdisk35 1147
hdisk36 1148
hdisk37 1149
hdisk38 114A
hdisk39 114B
hdisk40 114C
hdisk41 114D
hdisk42 114E
hdisk43 116B
hdisk44 116C
hdisk45 116D
hdisk46 116E
.....
The script below is to pull out the disk size.

HTML Code:
for disk in `pcmpath query device |awk 'BEGIN{print "DEVICE NAME\tSERIAL"} /DEVICE NAME/ {printf "%s\t", $5; getline; print substr($2, length($2)-3)}' |awk '{print$1}'`
do
printf "%s\t" $disk; bootinfo -s $disk
done
But, what I need to have is the sum of hdisks with the output:

HTML Code:
List of the output first:

DEVICE NAME   SERIAL            SIZE
hdisk28           110B               36864
hdisk29           1112               36864
hdisk30           1115               36864
hdisk31           1116               24576
hdisk32           1116               16384
......

and then add them up as Sub-Total and Grand Total followed by:

DISK SUMMARY in MB
SIZE: 36864 NUMBER OF DISKS: 3 SUB-TOTAL: 110592
SIZE: 24576 NUMBER OF DISKS: 1 SUB-TOTAL: 24576
SIZE: 16384 NUMBER OF DISKS: 1 SUB-TOTAL: 16384
....
GRAND TOTAL ======================== xxxxxxxx
Please let me me know how to get the disk size per each disk size and disk summary (SIZE: NUMBER OF DISKS: SUB-TOTAL & GRAND TOTAL)
# 2  
Old 02-03-2013
Why didn't you post the entire requirement in your recent post?
I don't know what the output of bootinfo -s looks like; I presume it's just one numerical field. If it's sth different, pls post here.
I don't have the slightest clue about on what to build the sub totals (can't be sheer size, can it).
Try this (untested; output needs some polishing):
Code:
awk 'BEGIN{print "DEVICE NAME\tSERIAL\tSIZE"}
     /DEVICE NAME/  {disk=$5
                     printf "%s\t", disk
                     getline; printf "%s\t", substr($2, length($2)-3)
                     ("bootinfo -s " disk) | getline; print; sum+=$0
                    }
     END {print "some nifty clever text:", sum}
    ' file


Last edited by RudiC; 02-03-2013 at 04:02 PM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-05-2013
That works perfect! Appreciate it!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help the sum from loop

Hi all, I have one host i need to run in loop to check the capacity from different frame and get the output to one file and sum it and convert to TB this is Code #!/bin/ksh DATE=`date '+%d%m%y'` for f in `cat /home/esx-capacity/esx-host.txt` do for g in `cat /home/esx-capacity/frame`... (10 Replies)
Discussion started by: ranjancom2000
10 Replies

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

3. Shell Programming and Scripting

Sum up numbers in a for loop

Hi i have to calculate some numbers, column by column. Herfore i used a for-loop.. for i in {4..26};do awk -F"," '{x'$i'+=$'$i'}END{print '$i'"\t" x'$i'}' file.tmp;done ----- printout ----- 4 660905240 5 71205272 6 8.26169e+07 7 8.85961e+07 8 8.60936e+07 9 7.42238e+07 10 5.6051e+07... (7 Replies)
Discussion started by: IMPe
7 Replies

4. Shell Programming and Scripting

Help with Associative array to sum & Average

Hi All, I got stuck up with shell script where i use awk. The scenario which i am working on is as below. I have a file text.txt with contents COL1 COL2 COL3 COL4 1 A 500 200 1 B 500 300 2 A 290 150 2 B 290 140 3 C 100 100 I could able to sum col 3 and col4 based on col1 using... (1 Reply)
Discussion started by: imsularif
1 Replies

5. Shell Programming and Scripting

PERL : Group & Sum in hash

Hi, I have a hash which is to be populated by reading data lines from a flat file. I am supposed to read fields 1-5 from the file and load them on to the hash such that fields 1-4 are going to be the hash key-set and field 5 is the hash value. Field 5 is a monetary amount and is supposed... (1 Reply)
Discussion started by: sinpeak
1 Replies

6. Shell Programming and Scripting

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 ... (1 Reply)
Discussion started by: number10
1 Replies

7. Shell Programming and Scripting

Sum values & compare with footer

Hi All, I have a file abc.txt with 3 fileds. Field 3 contains amount. Also at the end of file there is a Footer record, which contains total amount. I need to calculate total sum of these fields & need to compare it with footer record. I have serched in thi site, many has asked this... (7 Replies)
Discussion started by: Amit.Sagpariya
7 Replies

8. Shell Programming and Scripting

While loop - The sum seems to be local

Hi, I am trying to extracting the sum of all varibles listed in a file. The code is as follows ##### FILE1 ######## Value1:2 Value2:2 Value3:6 Value4:5 ##### shell script ###### #!/bin/sh total=0 (2 Replies)
Discussion started by: eagercyber
2 Replies

9. UNIX for Dummies Questions & Answers

write a script & sum contents!PLEASE HELP

Hello everyone!! I have to write a script in unix that reads a file which is in the format: 1%blabla%30 2%blabla2%50 1%blabla3%40 2%blabla4%10 and produce the total quantities of 1 and 2: "1 = 70 2 = 60" I am taught basic unix commands, so I must keep it simple, how can i do this using... (7 Replies)
Discussion started by: questionmaker25
7 Replies
Login or Register to Ask a Question