daily pastebin goal
11%
SHARE
TWEET

Untitled

a guest Jul 12th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. key=1
  3. while [[ -n $1 ]]; do
  4.         arg=$1
  5.         shift
  6.         case "$arg" in
  7.                 "-k") key=$1; shift ;;
  8.                 *) file=$arg ;;
  9.         esac
  10. done
  11. if [[ -n "$file" ]] && [[ ! -f "$file" ]]; then
  12.         echo "File, '$file' not found!"
  13.         exit 1
  14. fi
  15. [[ $file ]] && exec < "$file"
  16. awk -v c=$key '{sum+=$c}END{print sum}' -
  17.  
  18.  
  19. ##  USAGE
  20.  
  21. $ cat test
  22. 1 2
  23. 2 2
  24. 3 3
  25. 4 4
  26.  
  27. $ sum test
  28. 10
  29.  
  30. $ cat test | sum
  31. 10
  32.  
  33. $ sum -k 2 test
  34. 11
  35.  
  36. $ sum test -k 2
  37. 11
  38.  
  39. $ cat test | sum -k 2
  40. 11
  41.  
  42. $ sum blah
  43. File, 'blah' not found!
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top