How to add numbers

Hello.

I new to Shell Scripting.

I have a file and here is the output of the file.

1.1M
1.1M
3.3M
149K
61K
75K
144K
135K
82K
170K
327K
2.0M
219K
165K
1.1M
388K

I want to add them. Please anyone can help me with the script how to add them.

thanks

How you get that file? What is that you are trying to achieve ?

Hi...

Thanks for ur reply.

I am doing yum local repository. When i go to mirror.centos.org and look at the os , updates or addons repository it doesn't tell me the size of the repository.

But like in os directory there are files which have the size so i copy all the sizes for each every file in os directory and add them and come out of the size.

How i can get size of the centos repository?

thanks,

I think you are trying to get the total size of the directory!!!

try to use th du command:
du -sk dirname

iron,

thanks for replying me :slight_smile:
-----------------------
-----------------------

Here is the output of my Original file....I will post only few because whole file is big.

mageMagick-6.0.7.1-17.el4_6.1.i386.rpm                   20-Apr-2008 09:18  2.4M
 ImageMagick-c++-6.0.7.1-17.el4_6.1.i386.rpm               20-Apr-2008 09:18  135K
 ImageMagick-c++-devel-6.0.7.1-17.el4_6.1.i386.rpm         20-Apr-2008 09:18  176K
 ImageMagick-devel-6.0.7.1-17.el4_6.1.i386.rpm             20-Apr-2008 09:18  1.4M
 ImageMagick-perl-6.0.7.1-17.el4_6.1.i386.rpm              20-Apr-2008 09:18   80K
 arpwatch-2.1a13-12.el4_6.1.i386.rpm                       26-Jan-2008 07:57  172K
 audit-1.0.15-3.el4_6.1.i386.rpm                           26-Nov-2007 12:48  201K
 audit-libs-1.0.15-3.el4_6.1.i386.rpm                      26-Nov-2007 12:48   38K
 audit-libs-devel-1.0.15-3.el4_6.1.i386.rpm                26-Nov-2007 12:48   54K
 autofs5-5.0.1-0.rc2.55.el4_6.1.i386.rpm                   15-Dec-2007 12:40  731K
 autofs5-5.0.1-0.rc2.55.el4_6.2.i386.rpm                   21-Dec-2007 06:58  731K
 compat-openldap-2.1.30-8.el4_6.1.i386.rpm                 26-Nov-2007 12:48  222K
 compat-openldap-2.1.30-8.el4_6.2.i386.rpm                 01-Dec-2007 02:53  222K
 compat-openldap-2.1.30-8.el4_6.4.i386.rpm                 22-Feb-2008 08:34  222K
 cups-1.1.22-0.rc1.9.20.2.el4_6.5.i386.rpm                 26-Feb-2008 09:24  1.5M
 cups-1.1.22-0.rc1.9.20.2.el4_6.6.i386.rpm                 02-Apr-2008 08:19  1.5M
 cups-1.1.22-0.rc1.9.20.2.el4_6.7.i386.rpm                 08-May-2008 12:49  1.5M
 cups-devel-1.1.22-0.rc1.9.20.2.el4_6.5.i386.rpm           26-Feb-2008 09:24  146K
 cups-devel-1.1.22-0.rc1.9.20.2.el4_6.6.i386.rpm           02-Apr-2008 08:19  146K
 cups-devel-1.1.22-0.rc1.9.20.2.el4_6.7.i386.rpm           08-May-2008 12:49  147K
 cups-libs-1.1.22-0.rc1.9.20.2.el4_6.5.i386.rpm            26-Feb-2008 09:24  111K

Now i have a shell script i will run and i get this output.
Here is the code for shell script

#!/bin/bash

########################
cat os | awk '{print $4}' > os1

grep 'K$' os1 > os3
grep 'M$' os1 > os4


sed 's/[A-Z]$/ /g' os3 > os5
sed 's/[A-Z]$/ /g' os4 > os6

rm -rf os3 os4

Here is the output of os1 os5 & os 6 file created by above script.

os1: 
2.4M
135K
176K
1.4M
80K
172K
201K
38K
54K
731K
731K
222K
222K
222K
1.5M
1.5M
1.5M
146K
146K
147K
111K

output of os5:

135
176
80
172
201
38
54
731
731
222
222
222
146
146
147
111

output of os6:

2.4
1.4
1.5
1.5
1.5

My Question:
Now i have two files os5 and os6. How i can add numbers in os5 and get the total and how i can add numbersin os6 and get the total.

Do you need something like this?

4.3.4% cat file
 ImageMagick-6.0.7.1-17.el4_6.1.i386.rpm                   20-Apr-2008 09:18  2.4M
 ImageMagick-c++-6.0.7.1-17.el4_6.1.i386.rpm               20-Apr-2008 09:18  135K
 ImageMagick-c++-devel-6.0.7.1-17.el4_6.1.i386.rpm         20-Apr-2008 09:18  176K
 ImageMagick-devel-6.0.7.1-17.el4_6.1.i386.rpm             20-Apr-2008 09:18  1.4M
 ImageMagick-perl-6.0.7.1-17.el4_6.1.i386.rpm              20-Apr-2008 09:18   80K
 arpwatch-2.1a13-12.el4_6.1.i386.rpm                       26-Jan-2008 07:57  172K
 audit-1.0.15-3.el4_6.1.i386.rpm                           26-Nov-2007 12:48  201K
 audit-libs-1.0.15-3.el4_6.1.i386.rpm                      26-Nov-2007 12:48   38K
 audit-libs-devel-1.0.15-3.el4_6.1.i386.rpm                26-Nov-2007 12:48   54K
 autofs5-5.0.1-0.rc2.55.el4_6.1.i386.rpm                   15-Dec-2007 12:40  731K
 autofs5-5.0.1-0.rc2.55.el4_6.2.i386.rpm                   21-Dec-2007 06:58  731K
 compat-openldap-2.1.30-8.el4_6.1.i386.rpm                 26-Nov-2007 12:48  222K
 compat-openldap-2.1.30-8.el4_6.2.i386.rpm                 01-Dec-2007 02:53  222K
 compat-openldap-2.1.30-8.el4_6.4.i386.rpm                 22-Feb-2008 08:34  222K
 cups-1.1.22-0.rc1.9.20.2.el4_6.5.i386.rpm                 26-Feb-2008 09:24  1.5M
 cups-1.1.22-0.rc1.9.20.2.el4_6.6.i386.rpm                 02-Apr-2008 08:19  1.5M
 cups-1.1.22-0.rc1.9.20.2.el4_6.7.i386.rpm                 08-May-2008 12:49  1.5M
 cups-devel-1.1.22-0.rc1.9.20.2.el4_6.5.i386.rpm           26-Feb-2008 09:24  146K
 cups-devel-1.1.22-0.rc1.9.20.2.el4_6.6.i386.rpm           02-Apr-2008 08:19  146K
 cups-devel-1.1.22-0.rc1.9.20.2.el4_6.7.i386.rpm           08-May-2008 12:49  147K
 cups-libs-1.1.22-0.rc1.9.20.2.el4_6.5.i386.rpm            26-Feb-2008 09:24  111K
4.3.4% awk 'END{print s/1024"M"}{s+=$NF~/M/?($NF*1024):$NF}' file
11.7512M

Hi...

Sorry to reply late.

thank you so much radoulov for replying back to me. :b:

your awk command help me that's was i was looking for to get total in Megabytes. I assume same command will give me total in Gigabytes if the all the size in Megabytes.

As i am new to shell script now. I will work on a script so i can get total size of centos repository for example 1) base 2) updates 3) addons and 4) extras. No mirror list repositories have size of each repo on their site...i haven't found any of them.

Will post my script later this week.

Thanks everyone for help.
:):slight_smile:

No, it converts the values reported in MB to KB and at the end it outputs in MB. If you want to get the size in GB just change the code like this:

awk 'END{print s/1024/1024"G"}{s+=$NF~/M/?($NF*1024):$NF}' file

Or:

awk 'END{printf "%.2fG\n", s/1024/1024}{s+=$NF~/M/?($NF*1024):$NF}' file

thank you again redoule for clearing this out for me. :slight_smile:

You're absolutly correct code is working very fine for me.

thanks,