Wednesday, August 28, 2013

Fun with Bash :: Sums

I frequently find the need to sum up long lists of numbers from the Bash command shell. The following is a nice bit of code that sums up the bytes contained in a series of files and prints out the each sum in GB.

The current directory has files *-bytes.log that each contain a single column with a number of bytes on each row, example:

53824
247104
61776
53824
53824
53824
247104
53824
247104
542

The code contains an outer loop to iterate the files and an inner loop to process the contents of the files
for file in $(ls *-bytes.log); do
  sum=0;
  for num in $(cat $file); do
    let sum=$sum+$num;
  done
  echo "$sum/1024/1024/1024" | bc -l | xargs printf "$file %1.2f GB\n";
done

This is an alternative approach using an inner while loop to read the sums rather than cat'ing the file
for file in $(ls *-bytes.log); do
  sum=0;
  while read num; do
    let sum=$sum+$num;
  done < $file
  echo "$sum/1024/1024/1024" | bc -l | xargs printf "$file %1.2f GB\n";
done

Sample results
user1-bytes.log 3.81 GB
user2-bytes.log 4.75 GB
user3-bytes.log 1.40 GB
user4-bytes.log 2.03 GB
user5-bytes.log 5.01 GB
...

I've also found the following bit of code helpful in my $HOME/.bashrc to make quick calculations easy (this comes from a nixCraft Facebook post:

# nixCraft (on Facebook) calc recipe
# Usage: calc "10+2"
#   Pi:  calc "scale=10; 4*a(1)"
# Temperature: calc "30 * 1.8 + 32"
#              calc "(86 - 32)/1.8"
calc() { echo "$*" | bc -l; }

No comments: