One of the cool things about Ruby is its ability to behave like typical Unix command-line tools, to do things like (similar to the example from the official documentation):
$ echo "matz" | ruby -pe '$_.upcase!'
MATZ
Awk, on the other hand can perform an aggregation on lines from standard input, e.g., 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’d like to know if it’s possible to get Ruby to do what is being achieved by the Awk BEGIN and END blocks above so as to be able to do similar aggregation operations.
1 Answer