You can use this:
#!/bin/bash if [ ! -f $1 ]; then echo "File $1 not found" exit 1 fi number=$(grep total_points $1 | wc -l ) sumTotal=$(grep total_points $1 | awk '{sum+=$2} END { print sum }') sumToday=$(grep total_points $1 | awk '{sum+=$5} END { print sum }') echo "Total SUM: $sumTotal" echo -n "Total AVG: " echo "scale=5;$sumTotal/$number" | bc echo "Today SUM: $sumToday" echo -n "Today AVG: " echo "scale=5;$sumToday/$number" | bc
Then save the file, for example: script.sh
Change the permissions on the executable: chmod +x script.sh
Then run it: ./script.sh sample.txt
This will output:
Total Record: 3 Total SUM: 429.62 Total AVG: 143.20666 Today SUM: 103.52 Today AVG: 34.50666
Note: $1
will be the input file.
Here's some more help on the bc , grep , awk command
Book of zeus
source share