Dilu
Dilu

Reputation: 119

Shell Script to Multiply Lines and add the Sum

 Thanks all for the inputs but now i landed up in another problem .

  This is my Orginal syntax 

   db2 list tablespaces show detail | grep -e " Free pages" -e " Page size"
   Free pages                           = Not applicable
 Page size (bytes)                    = 4096
 Free pages                           = Not applicable
    Page size (bytes)                    = 4096
  Free pages                           = Not applicable
  Page size (bytes)                    = 4096
   Free pages                           = 36960
    Page size (bytes)                    = 32768
  Free pages                           = 40800
 Page size (bytes)                    = 16384
  Free pages                           = 22656
   Page size (bytes)                    = 4096
  Free pages                           = Not applicable
      Page size (bytes)                    = 4096

      Now if i see Not applicable then i should remove Not applicable line and as well as the proceeding line for that i used the below syntax 

  db2 list tablespaces show detail | grep -e " Free pages" -e " Page size"  | awk '/Not/{getline;next} 1'

  Free pages                           = 36960
 Page size (bytes)                    = 32768
 Free pages                           = 40800
  Page size (bytes)                    = 16384
 Free pages                           = 22656
 Page size (bytes)                    = 4096

But i am not sure why the last 4096 is comming for me in the output ??

and then i need to remove the (bytes) as well and print only the value so i used the below syntax

 db2 list tablespaces show detail | grep -e " Free pages" -e " Page size"  | awk '/Not/{getline;next} 1' | sed 's/(bytes)//g' | awk '{print$4}'

36960 32768 40800 16384 22656 4096

and i forgot to mention that with respect to my first posting (My question is i want to multiply line 1 to line 2 then line 3 to line 4 and line 5 to line 6 as goes on till the end of the output and finally add the whole sum )

in the second line , fourth line and sixth line i need to take only the first 2 charchater and then multiply

Like below

36960 * 32 40800 * 16 and goes on from that i need the value in the GB by dividing by 1024 and 1024 again as the orginal value is in KB

                 I know i am asking too much but can some one help ??
                     Waiting for some reply .. Thanks

Upvotes: 0

Views: 126

Answers (1)

konsolebox
konsolebox

Reputation: 75548

For a bash shell script:

#!/bin/bash
TOTAL=0
while read A && read B; do
    (( TOTAL += A * B ))"
done
echo "Total: $(( TOTAL / 1024 / 1024 )) GB"

Then run:

bash script.sh < input_file

The concept should be easy to convert if you want it to be usable across many types of shell. Just consider the command bc and other similar tools probably.

Upvotes: 1

Related Questions