My daughter said that 3 seems to be the most common last digit in primes. I thought it might be 7. Without even thinking too much about it, I banged this out in pretty much a "flow of thought" kind of way:
seq 1 100 | xargs -L1 factor | egrep '^([0-9]+): \1$' | grep -o '[0-9]$' | sort | uniq -c | sort -n -r
Sure you won't use this in any long term program but that's because it is **slow**, not because it is unreadable. Yes it is "calling out to 15 different programs", but they're either fairly common (grep, sort, uniq) or easy to guess what they do (seq, factor).
To be honest I've rarely used anything more than grep/egrep, cut, sort, and occasionally a line or two of sed or perl. Years later I can still understand all those scripts because these basic tools are still around and they're well known and stable.
I agree that this example is somewhat contrived for our discussion (though it was real enough for me at the time I wrote it), but the wins for shell, IMO, come from:
* the lack of any actual variables anywhere
* 'sort | uniq -c', which encapsulates what would be the second largest component of the processing if you did this in a proper language
* 'factor', although it needs to be filtered by a regex to throw out the non-primes
Copyright © 2018, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds