#!/bin/sh # Compute average of integer values in a file # Check for input parameter # The input file has lines of integers of the form: # ID STEPS COST if [ -z "$1" ]; then echo 'Usage: average.sh (bb|hr|dp)'; exit; fi if [ "$1" != "bb" -a "$1" != "hr" -a "$1" != "dp" ] then echo 'Usage: average.sh (bb|hr|dp)'; exit; fi # ----------------------------------- # Remove old files if [ -f ${1}.steps ]; then rm ${1}.steps; fi if [ -f ${1}.costs ]; then rm ${1}.costs; fi # ********************************** LOOP over all files for FILE in `echo $1*txt`; do # ------------------------------------ # 1. READ COLUMNS of the input file # Get the content of the input file (2nd and 3rd field) STEPS=`cut -d" " -f2 -s $FILE` COSTS=`cut -d" " -f3 -s $FILE` COUNT=`wc -l < $FILE` if [ -z "$STEPS" -o -z "$COSTS" ]; then echo 'Wrong file content'; exit; fi # ------------------------------------- # 2. SUM steps and costs from the previous step # Variables SUM_S=0; SUM_C=0; for NR in $STEPS; do SUM_S=`expr $SUM_S + $NR`; done; for NR in $COSTS; do SUM_C=`expr $SUM_C + $NR`; done; # ------------------------------------- # 3. Store results echo "count $COUNT steps: $SUM_S costs: $SUM_C" echo `expr $SUM_S / $COUNT` >> ${1}.steps echo `expr $SUM_C / $COUNT` >> ${1}.costs # End of "for all files" done # ********************************** LOOP-end