Here is a short (a bit brute) way to do it in bash:
calc() {
awk "BEGIN { print "$*" }";
}
IFS=$'\r\n' GLOBIGNORE='*' command eval 'memory=($(<'$1'))'
for (( i = 0; i < ${#memory[@]}; i++ )); do
echo "${memory[i]}" | awk '{print $1" "$3}' >> values.txt
total=$(awk '{ (Values += $2) } END { printf "%0.0f", Values }' values.txt)
length=$(awk '{print $2}' values.txt | wc -l)
echo "The average of hour $(awk '{print $1}' values.txt | tail -n1): $(calc ${total}/${length})"
done
rm values.txt
The result of the execution is the following:
ivo@spain-nuc-03:~/Downloads/TestStackoverflow$ ./processing.sh test.csv
The average of hour 201811270000: 3
The average of hour 201811270010: 3.5
The average of hour 201811270020: 4
The average of hour 201811270030: 5.25
The average of hour 201811270040: 5.8
The average of hour 201811270050: 5.16667
The average of hour 201811270100: 5.14286
The average of hour 201811270110: 4.625
The average of hour 201811270120: 4.88889
The average of hour 201811270130: 5
The average of hour 201811270140: 5
The average of hour 201811270150: 4.75
The average of hour 201811270200: 4.46154
ivo@spain-nuc-03:~/Downloads/TestStackoverflow$
You can later change the output to forward it to a file.
There are more elegant ways of doing this for experienced bash users.
For Paul Hodges:
Awk is to point to the specific column in question as we don't know if that column has the same length as the rest of the file (Still Applies).
tr -d is necesarry as the value of the variable needs to be an integer and not a string (Only at command line):
This is a string:
ivo@spain-nuc-03:~/Downloads/ScriptsClientes/BashReports/Tools/TextProcessing$ cat values.txt | wc -l
13
ivo@spain-nuc-03:~/Downloads/ScriptsClientes/BashReports/Tools/TextProcessing$
This is an integer:
ivo@spain-nuc-03:~/Downloads/ScriptsClientes/BashReports/Tools/TextProcessing$ cat values.txt | wc -l | tr -d '\n'
13ivo@spain-nuc-03:
Addtionally just doing wc -l file returns the following(Still applies):
ivo@spain-nuc-03:~/Downloads/ScriptsClientes/BashReports/Tools/TextProcessing$ wc -l values.txt
13 values.txt
ivo@spain-nuc-03:~/Downloads/ScriptsClientes/BashReports/Tools/TextProcessing$
Not at all suitable for the task at hand as it forces you to filter out the name of the file.
Please be sure before criticizing.