4 ms·
Isn't tr A-Z a-z | tr -cs a-z '\n' | sort | uniq -c | sort -rn | sed ${1}q equivalent to tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | se
by lqet 8y ago
Isn't
tr A-Z a-z | tr -cs a-z '\n' | sort | uniq -c | sort -rn | sed ${1}q
equivalent to
tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | sed ${1}q
but 4 characters shorter? Or am I missing something?
- garethrees 8y agoMcIlroy's claim was not that his script is the shortest possible way to implement the task, but rather that the Unix philosophy of combining general-purpose tools operating on text streams is a better engineering approach than Knuth's special-purpose program, because the general-purpose tools can be reused in many ways. McIlroy: "A first engineering question to ask is: how often is one likely to have to do this exact task’? Not at all often, I contend. It is plausible, though, that similar, but not identical, problems might arise. A wise engineering solution would produce—or better, exploit—reusable parts." [1] [1] https://www.cs.tufts.edu/~nr/cs257/archive/don-knuth/pearls-2.pdf https://www.cs.tufts.edu/~nr/cs257/archive/don-knuth/pearls-...
- lqet 8y agoMy initial comment was easy to misunderstand, sorry: I was just wondering whether the additional A-Z check was doing something I was not aware of.
- garethrees 8y agoIt's slightly more efficient to squeeze non-alphabetic characters first and then convert to lowercase, rather than vice versa, because in the former case most of the non-alphabetic characters only get processed once, compared to twice in the latter case.
- throway88989898 8y agowondering if there's a utility for finding shorter strings with the same output
- ColinWright 8y agoYes, it's shorter, but we're not playing golf. It's conceptually pretty much the same, and then you just go with whichever one you came up with first. Yours says: Convert to lower case Convert non-letters to CR The original says: Convert non-letters to CR Convert to lower case Conceptually the same. The point is, most people don't seem to be able to do this at all, although there are many who can tinker with a given solution and improve it around the edges. It's the ability to come up with the initial version that seems to be rare.
- darrenf 8y agoMy first reaction was to wonder why `tr` was required at all - neither conversion struck me as necessary. These days both `sort` and `uniq` can do case-insensitivity: sort -f | uniq -ic So I just allow the shell to do word splitting: for x in `cat foo.txt` ; do echo $x ; done | sort -f | uniq -ic | sort -rn | head -<num>
- ColinWright 8y agoDoesn't that have different behaviour if there is punctuation in the file? On my phone and not in a position to test it, so I'm not sure, but I think it's different.
- darrenf 8y agoIt does indeed. Good catch :)
- xorcist 8y agoDon't abuse command line parameters just to do word splitting. There's at least a dozen obvious ways to do that without leaving your reader guessing what that loop does. Avoid being clever. If you want to change word separators to line separators then just do that (with tr, sed, or some other way) and the purpose will be obvious. And please be mindful of looping over user supplied data like that. Those things are typically the ones that works fine in test but blow up in production. The environment has a maximum size, and you shouldn't store large amounts of data there.
- techbio 8y ago$ cat test.file | tr -cs A-Za-z '\n' | tr A-Z a-z > diffFirst $ cat test.file | tr A-Z a-z | tr -cs a-z '\n' > diffSecond $ diff diffFirst diffSecond $ No differences with my test file.