Re: The problem with one-liners...
by jmcnamara (Monsignor) on Feb 03, 2003 at 18:00 UTC
|
That's why I keep a file of one-liners and here it is:
| [reply] [Watch: Dir/Any] [d/l] |
|
Oh well, a little fiddling since it's such fun stuff:
# Last line of a file
perl -pe '$*=$_}{$_=$*' file
-> perl -pe '$\=$_}{' file
Which leads me to a head-tail (useful eg, for finding out the range of a log file). perl -pe '$*||=$_; $\=$_}{$_=$*' file
and (not stopping while he's ahead) . . .
# Exit after first match
perl -ne 'print; last if /foo/' file
-> perl -pe '/foo/...last' file
  p | [reply] [Watch: Dir/Any] [d/l] [select] |
|
That's a nice collection. It would be cool if people would post some of their favorite one-liners to the snippets section.
| [reply] [Watch: Dir/Any] |
|
In relation to this node I'd like to point out that these one-liners aren't always the ones that I use to accomplish a task. For example there are several head(1) and tail(1) variations. However, I never use these, I always use the system commands instead.
In addition it doesn't contain all of the one-liners that I ever use. There are many of the variety "print all lines greater than 72 characters" or "print the third field of every line" that I can easily reproduce and which I don't have to record.
Instead this list contains some longer one-liners that I might not remember, such as the module version one, and a lot of quirky variations that might be useful in golf or elsewhere. As such it serves me more as a sketchbook of ideas for future work than as a completed reference.
--
John.
| [reply] [Watch: Dir/Any] |
|
perl -e 'require Some::Module;'
(golf, anyone?) for checking for the existence of some module, but then I realized that the intent of yours was probably to produce a "boolean" value, which could be more useful to something like an install script than mine, which will either produce nothing or some text. | [reply] [Watch: Dir/Any] [d/l] |
Re: The problem with one-liners...
by ChemBoy (Priest) on Feb 03, 2003 at 17:32 UTC
|
Unless, of course, the solution was saved somewhere... I maintain a file called oneliners.txt in my perl source directory, and it's saved me all kinds of pain and suffering. One or two of the entries might do better as 5-10 line scripts, but then again, they might not--and I certainly wouldn't do better with another however many (about a dozen at the moment) random single-purpose scripts lying around for me to keep track of.
If God had meant us to fly, he would *never* have given us the railroads. --Michael Flanders
| [reply] [Watch: Dir/Any] |
Re: The problem with one-liners...
by perrin (Chancellor) on Feb 03, 2003 at 17:46 UTC
|
They are saved in my (searchable) bash history, and I put the good ones into files so I can run them with ". filename" when I need them again. | [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: The problem with one-liners...
by greenFox (Vicar) on Feb 04, 2003 at 05:17 UTC
|
Like others I have both a shell history and a one-liners file, so anything particularly clever gets remembered somewhere :) Constructing on the fly though is half the fun of one-liners for me, and the repetition has improved my familiarity/knowledge of the language. It feels good when I have solved a problem (one-liner or scripted) without having to reach for a man page, text or WWW search.
How long before someone posts a suggestion for a new section called "one-liners"? <grin>
-- Life is a tale told by an idiot -- full of sound and fury, signifying nothing. William Shakespeare, Macbeth
| [reply] [Watch: Dir/Any] |
Re: The problem with one-liners...
by Aristotle (Chancellor) on Feb 04, 2003 at 14:19 UTC
|
I throw away 98% of my oneliners, so I have never noticed this problem. Were this a concern, though, I'd have something like this in my .bashrc (may need minor fiddling before it actually works):
function ol {
local NAME="$1"
shift
eval perl $(grep '^'"$1"'\>' ~/oneliners|cut '-d ' -f2-) "$@"
}
accompanied by a file oneliners in my home looking like this (assuming some snippets from jmcnamara's collection):
col1 -naleshift@F;print"@F"
rmcr -i -pes/\r//
2spc -pe$_.=$/
(Yes, the quotes are missing; this is due to the fact that the line is passed back out of the command substitution as a single string). And then I could just do something like
$ ol rmcr file1 file2 file
Makeshifts last the longest. | [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |
Re: The problem with one-liners...
by steves (Curate) on Feb 03, 2003 at 22:51 UTC
|
If I think I'll want to do it again I script and save it from the start. Otherwise it goes into my ksh history buffer where I have time to override that decision.
I don't agree with this premise in general. I probably rip out a dozen or two shell/awk/sed/Perl one-liners a day that are solving a very specific problem at hand that it makes no sense to abstract to take arguments, bullet proof, etc. Not all code is meant to be reusable. What's reusable is your ability to recognize and solve the problem at a meta level.
Plus I like the looks you get from neophytes when you start scripting right at the command line ...
| [reply] [Watch: Dir/Any] |
(jeffa) Re: The problem with one-liners...
by jeffa (Bishop) on Feb 05, 2003 at 18:15 UTC
|
I too save one-liners in a text file, but i
really feel that if i can't come up with a one-liner in
about the same time it takes me to post a short reply here
at PM, then i have no business using a one-liner for the
task at hand. One-liners are stream of thought conciousness,
so to speak - true throw-away scripts. Anything else should probably be in a file that includes strict and warnings.
jeffa
perl -lpe '}{$_=$.' somefile
| [reply] [Watch: Dir/Any] [d/l] |
My Conclusions (Re: The problem with one-liners)
by John M. Dlugosz (Monsignor) on Feb 04, 2003 at 06:19 UTC
|
Many people save their history. The command shell I use, 4NT, also has logging options. However, if I saved everything, what I'm interested in might get lost in the forest of meaningless day-to-day commands.
So, I think I'll turn logging on when I'm developing a "one liner" to have the work automatically saved, then turn it off again. If I need to do something again, I can find the last line of the bunch of similar lines and copy that out, and save it to a one-liner file.
—John | [reply] [Watch: Dir/Any] |
|
$ grep '^ *perl' .bash_history
C:\> perl -lne"/^\s*perl/&&print" history.txt
Makeshifts last the longest. | [reply] [Watch: Dir/Any] [d/l] |
Re: The problem with one-liners...
by insensate (Hermit) on Feb 05, 2003 at 02:17 UTC
|
$ perl -e'print "Just Another Perl One-Liner\n"'
Just Another Perl One-Liner
$ history -n -1 | head -1 >> ~/oneliners.txt
Or something like that ;^) ...I even
alias keep='history -n -1 | head -1 >> ~/oneliners.txt'
In my .kshrc...that makes it pretty slick | [reply] [Watch: Dir/Any] [d/l] [select] |
|
I was just about to suggest the above solution and then decieded to read all the posts... and there it was. My 'keep' is very similar: Once I get a perl one-liner that works how I wanted it to, I add it to my oneliners file. The only difference being that my 'keep' lets me type things like:
machine> perl -le 'print map{(a..z)[rand 26]} 0..rand 10'
kjfgsdkjg
machine> keep Generate a random word
which adds the following to my oneliners file:
# Generate a random word
perl -le 'print map{(a..z)[rand 26]} 0..rand 10'
I'm on a different machine here so when I get to the right machine, I'll stick the keep command on my scratchpad.
Thanks to jmcnamara for the one-liner. I don't have my list with me. His is at Re: The problem with one-liners... | [reply] [Watch: Dir/Any] [d/l] [select] |
|
I like that idea. I'll adapt that, and add a time stamp as well.
| [reply] [Watch: Dir/Any] |
Re: The problem with one-liners...
by zentara (Archbishop) on Feb 04, 2003 at 15:48 UTC
|
I think a "1-liners section" would be useful and widely used addition to
perlmonks. | [reply] [Watch: Dir/Any] |
Re: The problem with one-liners...
by haxordan (Novice) on Feb 04, 2003 at 16:55 UTC
|
From a UNIX/Linux point of view, if your shell history is large enough, you should have access to that one-liner. If I think I will need a one-liner in the future (and even sometimes when I don't), I'll just dump it to a file.
--
haxordan
If the world is to end in a whisper, I hope that it's not someone whispering,"I wonder what this button does?" -DTB
| [reply] [Watch: Dir/Any] |
Re: The problem with one-liners...
by Juerd (Abbot) on Feb 06, 2003 at 18:58 UTC
|
I throw away one-liners. Anything I can do today, I hope to still be able to do in a year or two. By (my) definition, one-liners are bad code and bad code should imho not be saved :)
My favourite is
perl -ple'$a=$_=eval'
I dislike bc. The perl one-line calculator does what I want and is simple enough to remember and type in.
Juerd
- http://juerd.nl/
- spamcollector_perlmonks@juerd.nl (do not use).
| [reply] [Watch: Dir/Any] [d/l] |