Operating System - HP-UX
1833875 Members
2915 Online
110063 Solutions
New Discussion

[Q] shell programming - How to sum contents within list file using shell programming ?

 
SOLVED
Go to solution
Tony, Lim
Frequent Advisor

[Q] shell programming - How to sum contents within list file using shell programming ?

I Occasionally might make list file only contain file size, like this.

# ls -l |awk '{print $5}' > list.txt

The result is like belows ..

2074654
11341023
11940435
558066
9160
10
201
1975
1894

What I want to do is to sum this contents as numbers to calculate total summation.

Could you let me know how to sum this contents using shell programming ??
9 REPLIES 9
Senthil Kumar .A_1
Honored Contributor

Re: [Q] shell programming - How to sum contents within list file using shell programming ?

Hi tony,

Just do the following

ls -l|awk '{print $5}' | awk 'BEGIN {sum=0} {sum+=$1;print sum}'

You got ur solution.

Regards,
Senthil Kumar .A
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)
Senthil Kumar .A_1
Honored Contributor

Re: [Q] shell programming - How to sum contents within list file using shell programming ?

The last number in the output of the above command will be your Grand total(sum). You do not require to create a list.txt.

Even simpler is

ls -l | awk 'BEGIN {sum=0} {sum+=$5;print sum}'

This is even better..for u do not require 2 awk statements.

Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)
Peter Godron
Honored Contributor

Re: [Q] shell programming - How to sum contents within list file using shell programming ?

Tony,
please note:
senthil's excellent solution appears to have a problem if sub-directories exist.
Senthil Kumar .A_1
Honored Contributor

Re: [Q] shell programming - How to sum contents within list file using shell programming ?

Hi Tony,

I agree with peter,

U could use, command 'du -sk' instead for a disrectory.

If ur intension is just to sum up the fifth field, a much more elegent way is..

ls -l | awk 'BEGIN {sum=0} {sum+=$5} END {print sum}'
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)
Victor BERRIDGE
Honored Contributor

Re: [Q] shell programming - How to sum contents within list file using shell programming ?

Hi,
There many solutions for summation in shells, here Im using your list:
ant:/home/vbe $ tot_list
Total : 25927418 bytes
----
ant:/home/vbe $ more tot_list
count=0
for input in `cat list.txt`
do
count=`expr $count + $input`
done

print "Total : $count bytes \n"

---

All the best
Victor
Muthukumar_5
Honored Contributor

Re: [Q] shell programming - How to sum contents within list file using shell programming ?

Use like,

ls -l | awk -v sum=0 '{ sum+=$5 } END { print sum; }'

--
Muthu
Easy to suggest when don't know about the problem!
Arturo Galbiati
Esteemed Contributor

Re: [Q] shell programming - How to sum contents within list file using shell programming ?

Hi,

using your command directly by awk:
ls -l | awk '{t+=$5};END {printf "%12d\n",t}'

More in general:
awk '{t+=$1};END {printf "%12d\n",t}' list.txt

where $1 is the field you want to summ

HTH,
Art
Muthukumar_5
Honored Contributor

Re: [Q] shell programming - How to sum contents within list file using shell programming ?

If you want to know the disk usage of directory and sub level directories then simply use as,

# du -ks

It is returning KB size.

If you want to have the current directory files (without sub directory) then we can use,

# ls -l | awk -v sum=0 '{ sum+=$5; } END { print "Total Size in bytes are "sum }'

--
Muthu
Easy to suggest when don't know about the problem!
James R. Ferguson
Acclaimed Contributor
Solution

Re: [Q] shell programming - How to sum contents within list file using shell programming ?

Hi:

If all you want to count are the sizes of *files* in a simple 'ls' output, simply do:

# ls -l | awk '/^-/ {SUM+=$5};END{printf("%-12.0f\n",SUM)}'

If the first field doesn't begin with a "-" then it's not a regular file and we skip it.

Regards!

...JRF...