Need help in finding sum for values in 2 different fields

Hi there,

I have 2 files in following format
cat file_1

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

 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

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.

awk:

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)
}' b a

python

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:
    leo+=int(words[2])awk
for i in sorted(leo):
 print(i,leo)
	
1 Like

thanks for the reply it worked..