One of the most interesting things in Ruby is the ability to behave like typical Unix command line tools to do things like (similar to the example in the official documentation):
$ echo "matz" | ruby -pe '$_.upcase!' MATZ
Awk, on the other hand, can perform aggregation on lines from standard input, for example, by summing a sequence of numbers:
$ for (( i=0; $i < 50; i++ )); do echo $i; done | awk 'BEGIN { tot=0; } { tot += $0 } END { print tot }' 1225
I would like to know if it is possible to get Ruby to do what is achieved using the above Awk BEGIN and END blocks in order to be able to perform similar aggregation operations.
source share