Advertisement
dzocesrce

[OS] Average Electricity Usage

Jan 21st, 2024
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.71 KB | None | 0 0
  1. #!/bin/bash
  2. if [ $# -ne 1 ]
  3. then
  4.     echo "USAGE: `basename $0` mesec"
  5.     exit 1
  6. fi
  7. MONTH=$1
  8. total_electricity=`cat electricity.txt | grep "|$MONTH|" | awk -F\| 'BEGIN {total=0} {total+=$5} END {print total}'`
  9. number_people=`cat electricity.txt | grep "|$MONTH|" | wc -l`
  10. if [ $number_people -eq 0 ]
  11. then
  12.     echo "Nema podatoci za toj mesec"
  13.     exit $1
  14. fi
  15. average_electricity=`expr $total_electricity / $number_people`
  16. touch people.txt
  17. #just testing if I can put a shell variable into an awk command.
  18. #it looks good on paper, but unfortunately all I have is a laptop
  19. echo "`cat electricity.txt | grep "|$MONTH|" | awk -var $average_electricity '$3>$var {print $1 $2}'`" >> people.txt
  20. cat people.txt
  21.  
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement