The data file can be downloaded from here: months.txt.gz. To unpack it, type:
gunzip months.txt.gz
There are several solutions to this exercise.
One can observe that the months which names end with 'er' are the last four months and can use tail
command to select the four last lines from the file:
cat months.txt | tail -n 4 | numsum -c -x 3
This solution uses numsum
command to calculate the sum of numbers in the third column (on Debian it requrires the package 'num-utils' to be installed).
One can also use the grep
utility to select the lines with substring 'er':
cat months.txt | grep 'er' | numsum -c -x 3
If numsum
utility is not available one can use one-liner1), for instance in AWK language:
cat months.txt | grep 'er' | awk '{s=s+$3} END {print s}'
It is also possible to use Octave to calculate the sum, though in this case, one has to save the numbers in a temporary file:
cat months.txt | grep 'er' | cut -s -d " " -f 3 > tmpMonths octave -q --eval "disp(sum(load('tmpMonths'))); exit"