3 ms·
> The hard part on the path to becoming a command line wizard, or any kind of wizard, is thinking about how to apply existing tools to your particular problems.
by dahart 8y ago
> The hard part on the path to becoming a command line wizard, or any kind of wizard, is thinking about how to apply existing tools to your particular problems. You could memorize McIlroy’s script and be prepared next time you need to report word frequencies, but applying the spirit of his script to your particular problems takes work.
It definitely takes a bit of work, but hopefully it's still a lot less work than writing the verbose solution.
The hard part of mastering unix one-liners for me are two things:
- Figuring out which tool can pick apart or reconstruct a specific syntax robustly. I think I spend more time figuring out input parsing and output presentation than anything else.
- Knowing when the right time is to ditch the clever one-liner and upgrade to a real program. There's a benefit to prototyping as a one-liner, but there are cases when replacing the result with a long program is appropriate, especially in a production environment. But, then again, only if the one-liner isn't robust or scalable enough, and usually it probably is.
- dmitriid 8y agoA verbose solution is repeatable, devuggable, and understandable six months or even six years from now. A verbose solution can be deconstructed and understood by a newbie. A oneliner? Not so much, if at all.
- dahart 8y agoThat is occasionally true, but in 30 years I've absolutely definitely seen a lot more the other way around, personally speaking. Good POSIX one-liners don't need to change usually and aren't hard to understand. A verbose program, on the other hand only lasts if it's like K&R C with zero dependencies. All the languages and libraries have changed considerably, scripting languages and versions come and go, C++ is quite different and might not compile later, build systems have backward incompatibilities. Plus, a one liner is, like, one line. It's really not that hard to understand unless it's intentionally crazy, and even then, even if it takes a half hour to work through, that's still less work than something like Knuth's 15 pages of code. We could be imagining pretty different scenarios, though, so feel free to share examples. Normally a pipe consisting of a combination of sort, cut, head, and uniq is trivial to grok, and it would be very wasteful to re-implement that kind of thing on your own. Here are a few one-liners I've made use of recently that I love: # count the function calls in log.txt grep -Po '^\K.+(?=\()' log.txt | sort | uniq -c | tee callcounts.txt # sum the timings # input: 2 columns, time & name. output: 3 columns: time_sum, name count, name # It's basically a pivot table in one line of code < time.txt perl -lane '$k{$F[1]}+=$F[0]; $c{$F[1]}+=1; END{print "time\tcount\tcall"; print "$k{$_}\t$c{$_}\t$_" for keys(%k) }' | sort -n | tee time_grouped.txt In my own personal repo, the bash scripts and one liner aliases are outlasting all my python and C++ code. The verbose programs that I wrote myself are harder for me to maintain.