Skip to content

Instantly share code, notes, and snippets.

@egulhan
Created January 13, 2014 15:00
Show Gist options
  • Save egulhan/8401786 to your computer and use it in GitHub Desktop.
Save egulhan/8401786 to your computer and use it in GitHub Desktop.
Scripting methods to sum numbers in a file
Scripting methods to sum numbers in a file:
-------------------------------------------
- awk '{s+=$1} END {print s}' number-list-file
- paste -s -d+ number-list-file|bc
- sum=0; while read num ; do sum=$(($sum + $num)); done < number-list-file ; echo $sum
- php -r '$sum=0;foreach(file("number-list-file") as $line) $sum+=$line;echo $sum.PHP_EOL;';
- cat number-list-file | php -r "echo array_sum(explode(PHP_EOL, stream_get_contents(STDIN)));"
- cat number-list-file | xargs | sed -e 's/\ /+/g' | bc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment