- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- [Q] shell programming - How to sum contents within...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2006 07:44 PM
02-20-2006 07:44 PM
# 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 ??
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2006 07:55 PM
02-20-2006 07:55 PM
Re: [Q] shell programming - How to sum contents within list file using shell programming ?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2006 07:58 PM
02-20-2006 07:58 PM
Re: [Q] shell programming - How to sum contents within list file using shell programming ?
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2006 08:08 PM
02-20-2006 08:08 PM
Re: [Q] shell programming - How to sum contents within list file using shell programming ?
please note:
senthil's excellent solution appears to have a problem if sub-directories exist.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2006 08:12 PM
02-20-2006 08:12 PM
Re: [Q] shell programming - How to sum contents within list file using shell programming ?
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}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2006 08:26 PM
02-20-2006 08:26 PM
Re: [Q] shell programming - How to sum contents within list file using shell programming ?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2006 09:09 PM
02-20-2006 09:09 PM
Re: [Q] shell programming - How to sum contents within list file using shell programming ?
ls -l | awk -v sum=0 '{ sum+=$5 } END { print sum; }'
--
Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2006 11:33 PM
02-20-2006 11:33 PM
Re: [Q] shell programming - How to sum contents within list file using shell programming ?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2006 11:48 PM
02-20-2006 11:48 PM
Re: [Q] shell programming - How to sum contents within list file using shell programming ?
# 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2006 11:52 PM
02-20-2006 11:52 PM
SolutionIf 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...