Unix Fizz-Buzz one-liner

Given that “implement Fizz-Buzz in a language of your choice” is quite a common programming interview task, I was wondering if it would be possible to do it in one line on the command line using Unix tools.

Unsurprisingly it is; here’s my version:

seq 100 | awk '{ if ($0 % 15 == 0) { print "fizzbuzz" } else if ($0 % 3 == 0) { print "fizz" } else if ($0 % 5 == 0) { print "buzz" } else { print $0 } }'

The seq at the beginning generates numbers from 1 to 100, and then the awk command does a basic if-else on each line to decide what to print.

It runs a bit faster than I was expecting – just under 2 seconds for a million numbers according to time.


View post: Unix Fizz-Buzz one-liner