On 3/21/06, Timothy Johnson <[EMAIL PROTECTED]> wrote: > > We run into one of these "How do I do this in a one-liner?" questions > pretty frequently, and I for one have to ask, what exactly makes the > one-liner so compelling, especially when you are using it for something > that will be run repeatedly? > > IMHO it would have been much more practical to just create a three line > Perl script and run it from the command-line. It makes it easier to > read, doesn't discourage commenting, and avoids the contortions > sometimes needed to cram it all into one line. >
I can only speak for myself here, but there are a couple of things that make one-liners appealing. By and large, they're the same things that drive a decision to use sed over awk, although these days I pretty much use perl for everything except maintaining bits of old code. 1) Quantity. If you're doing this sort of thing, you're probably doing it a lot. And you're probably searching the same file many times for slightly different information. Or you're searching a number of oh-so-subtly different log files for the same information. Having 400 different scripts that all do essentially the same thing with different regexes doesn't make sense. writing a general purpose tolls that will except the regex on the command line doesn't make a lot of sense, either: if you're going to type the regex anyway, you might as well just type out the command. 2) Clutter. given the choice, for simple tasks, I'll choose to clutter up my brain, rather than ~/bin. Not to mention that i may want to run the script as several different users, depending on the situation, and I certainly don't want all my personal scripts laying around in /usr/bin. 3) Sanity. What do you name all your former oneliners? ApacheLogReportOne, ApacheLogReportForBossThisWeeksParams03102006? That gets old, not to mention headache-inducing, in a hurry. 4) Platform. Most cases where you'd want a oneliner aren't work working up a truly robust script that looks at %ENV to figure out what system I'm on, etc., and how to deal with the quirks in the different log files, etc. 5) Laziness (in the good sense). I have to type a command line anyway. If it would only take a line or two of code (make it three with the bang path), and I'm still going to have to type a line to start it up, it's probably not worth my while to write a script, especially since I then have to deal with the input, etc. Let perl deal with $^I, <>, and maybe even print and split for me. How many lines of code would it take to emulate "perl -lpi -e's/^.*(match).*$/$1/replaces at least 5 lines of code right off the bat. the -l switch is particularly sneaky. 6) Testing. If you're testing a regex and are making incremental changes, things go a lot faster. I figure I save somewhere int he neighborhood of 60% of my keystrokes when I can do something small right on the command line, instead of constantly launching shells in emacs, or going through the constant "[run], fg, [edit], ^X^S, ^Z, [up][up], [run]" cycle. At the command line, that turns into "ret., [up], [edit], ret.". The only drawback to oneliners (for things that can legitimately be condensed to one line without qualifying for an obfuscation prize), then, is the difficulty remembering and formulating regexen. And why not get good at that? -- jay -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!