Need help in finding sum for values in 2 different fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in finding sum for values in 2 different fields
# 1  
Old 05-06-2013
Need help in finding sum for values in 2 different fields

Hi there,

I have 2 files in following format
cat file_1

Code:
Storage Group Name:    aaaa
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------ 
    0               21
Storage Group Name:    bbbb
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------ 
    0               9
Storage Group Name:    cccc
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------ 
    0               1001
Storage Group Name:    dddd
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------ 
    0               143
    1               429
Storage Group Name:    eeee
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------ 
    0               175
Storage Group Name:    ffff
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------ 
    0               4290
    1               4292
    2               4293
    3               4294
    4               4291
    5               4295
Storage Group Name:    gggg
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------ 
    0               19
    1               20
Storage Group Name:    hhhh
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------ 
    0               0
    1               1
    2               2
    3               3
    4               4
    5               5
Storage Group Name:    iiii
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------ 
    0               38

cat file_2
Code:
 8109 143
 520 528
 121 440
 8107 143
 8108 143
 8131 20
 2004 400
 2015 40
 2018 1000
 2026 120
 125 200
 22 935
 2010 40
 2016 40
 2024 125
 8097 5
 2014 450
 2022 70
 2001 200
 530 200
 2023 130
 2025 60
 2003 50
 2019 450
 2027 198
 2000 500
 2011 50
 8034 169
 1200 85
 8149 12
 9 100
 8079 55
 8111 143
 8059 67
 8143 12
 21 935
 8175 500
 505 200
 8133 20
 8049 50
 138 60
 8132 20
 8110 143
 49 131
 8112 143
 143 429
 525 21
 1001 165
 529 51
 2021 100
 8095 200
 2009 125
 2013 130
 2028 165
 2012 120
 2020 100
 2002 50
 8096 200
 2008 15
 8098 5
 2017 110
 8117 20
 8069 55
 8116 20
 522 220
 514 220
 510 220
 8129 83
 429 429
 518 220
 8118 20
 200 268
 4292 429
 7997 127
 4294 429
 4295 429
 175 250
 4290 429
 4291 429
 4293 429
 7999 100
 180 200
 13 69
 8044 200
 8187 20
 19 60
 8186 20
 8185 20
 11 69
 8038 175
 8037 175
 1850 99
 8189 268
 8191 268
 1000 198
 455 99
 2310 99
 54 20
 210 232
 8190 268
 39 231
 7998 100
 8089 225
 102 198
 220 450
 38 429
 1601 200
 8090 225
 8092 225
 250 200
 2101 120
 8091 225
 8009 132
 8119 50
 8120 50
 8121 50
 2205 100
 8094 200
 1904 150
 130 200
 8023 380
 90 40
 29 50
 24 20
 154 69
 28 399
 159 1
 15 90
 20 60
 235 619
 169 33
 8179 5
 23 210
 1201 50
 8024 380
 1903 25
 111 120
 8093 200
 190 400
 2200 130
 8020 33
 8021 33
 8099 50
 8019 33
 109 2
 2104 10
 120 198
 8029 66
 8043 200
 8138 6
 2100 178
 8139 6
 1602 198
 8039 175
 8040 175
 201 700

In file_1 I have host name aaaa,bbbb,cccc,dddd etc... below them i have some values assigned to that host name below UID
eg: aaaa has 21 and bbbb has 9 ,cccc has 1001, dddd has 143,429 , ffff has 4290,4292,4293,4294,4291,4295 etc.....
file_2 has values assigned to them first column values are the onces which should be used as reference i.e for aaaa-> 21 in file_2 next to 21 we have 935 for some hosts we have many values.
i.e 0-- 11

This is what I want from the two files.
I want to get the host name and get the sum of values from file_2
my final o/p should be in the following format.



etccc
Code:
HOST_NAME  TOTAL_CAPACITY_IN_GB)
aaaa                    935  
bbbb                   100
cccc                     165
dddd                     858(429+429)
eeee                      250
ffff                         3003

Please let me know if I am not clear. Thanks in advance.
# 2  
Old 05-07-2013
awk + python

awk:
Code:
gawk '{
if(NR==FNR){
        _[$1]=$2
}
else{
        if(/Storage/){
                name=$NF
        }
        else{
                if(NF==2){
                        l[name]+=_[$2]
                }
        }
}
}
END{
 for(i in l)
  print(i,l[i])
}' b a

python
Code:
import re
dict={}
leo={}
with open("a.txt") as f:
 for line in f:
  obj=re.match("Storage Group Name\s*:\s*(\S+)",line)
  if obj:
   name=obj.group(1)
   leo[name]=0
  else:
   obj=re.match("\s*\d+\s*(\d+)",line)
   if obj:
    dict.setdefault(name,[]).append(obj.group(1))
with open("b.txt") as f:
 for line in f:
  words=line.split(" ")
  for i in dict:
   if words[1] in dict[i]:
    leo[i]+=int(words[2])awk
for i in sorted(leo):
 print(i,leo[i])

This User Gave Thanks to summer_cherry For This Post:
# 3  
Old 05-07-2013
thanks for the reply it worked..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum fields of different files using awk

I'm trying to sum each field of the second column over many different files. For example: file1: file2: 1 5 1 5 2 6 2 4 3 5 3 3 To get: file3 1 10 2 10 3 8 I found answer when there are only 2 files as... (10 Replies)
Discussion started by: rogeriog.em
10 Replies

2. Shell Programming and Scripting

To perform sum aggregation on numeric fields

Hi all, I have following scenario to perform sum aggregation on certain columns Node Allocated_Space Pool_Name CS_Group Utilized Space -------- ---------------- ---------- --------- -------------- bdw1a_lun01 300 bdw_p0 ... (2 Replies)
Discussion started by: ckwan
2 Replies

3. Shell Programming and Scripting

Finding Sum of Occurrances

Hello friends, I have a database export and with this export i need to find some statistics which is required. I take the export of a table with only 3 columns like this: 2nd 3rd 12th 123 05.11.2010 09:27 0 124 05.11.2010 09:28 0 125 05.11.2010 09:34 1 126 05.11.2010 09:39 0... (5 Replies)
Discussion started by: EAGL€
5 Replies

4. Shell Programming and Scripting

How to sum up two decimal values?

I am running the following script : cat ind_sls_extr_UX.out_sorted | while read each_rec do count=`echo "${each_rec}" | cut -c1-2` if then final_amount=0 amount=`echo "${each_rec}" | cut -c280-287` echo "${amount}" final_amount=`expr ${amount} + ${amount}` ... (7 Replies)
Discussion started by: mady135
7 Replies

5. Shell Programming and Scripting

Finding the sum of two numbers

cat *.out |grep "<some text>" | awk '{print $6}' For ex,This will reutrn me 11111 22222 is it possible to add these two numbers in the above given command itself?I can write this to a file and find the sum. But I prefer to this calculation in the above given line itself. Any... (3 Replies)
Discussion started by: prasperl
3 Replies

6. Shell Programming and Scripting

how to sum values from 2 different files?

Hi I am trying to add count values from two different files into one file. Could any body please suggest me best command to do this? My problem was as follows: a.txt b.txt c.txt 10 20 30(needed) i tried cat a.txt b.txt > c.txt (its not adding the values) Thanks in advance.. Praveen (8 Replies)
Discussion started by: npk2210
8 Replies

7. Shell Programming and Scripting

How to sum column 1 values

I have a file file like this. I want to sum all column 1 values. input A 2 A 3 A 4 B 4 B 2 Out put A 9 B 6 (3 Replies)
Discussion started by: suresh3566
3 Replies

8. Shell Programming and Scripting

sum(col) finding from a file

Hi I have an file which looks like country address phone amount sweden |address |phone | 10 | Singapo |address |phone | 20 | Italy-N |address |phone | 30 | denmar |address |phone | 40 | Here i need to do the sum(amount), how to do this in shell scripting Thanks Babu (11 Replies)
Discussion started by: ksmbabu
11 Replies

9. Shell Programming and Scripting

calculating sum of fields in a file

Hey, I have a file and it has only one field. I need to calculate the sum of each filed as total. For e.g my file is 1 2 3 4 5 I need to calculate the total sum as 15. Please let me know how i can do it? (4 Replies)
Discussion started by: dsravan
4 Replies

10. Shell Programming and Scripting

sort sum fields

HI ALL, i have a problem when i do a sort sum with many fields. Is there a limit for fields? Do you know a solution? thanks in advance. the shell is: # SORT1 SORT1_rcode=777 if ; then echo "USE $DARSEQ/OTPU.FTPEPREC RECORD F,1000 " > $DARPARSRT/TPEKL508.SORT1_$$.srt ... (6 Replies)
Discussion started by: tullo
6 Replies
Login or Register to Ask a Question