Hi Raju, Sucks ...you sent this post when I was traveling. I saw this just before I shut the laptop and I /knew/ I could p0wn this if only i had some free time. Well now I do, so here goes ...
Firstly disclaimer: this is all in good-natured jest. On 08/19/2009 04:48 PM, Raj Mathur wrote: > On Wednesday 19 Aug 2009, steve wrote: >> [asnip] >> b. Learn a scripting language (I'd recommend python) -- >> http://wiki.python.org/moin/BeginnersGuide > > Just playing Devil's Advocate here, but the day I start recommending > Python as a scripting language is the day it lets me do something like > this from a command-line: > ORLY ? > perl -e 'undef$/;$"=chr(10);foreach(@ARGV){open(F,$_); > $l=<F>;@l=reverse(split(/\n/,$l));close(F);open(F,">$_");print F > "@l";close(F);}' file1 file2 file3... > python -c 'import sys for f in sys.argv[1:]: l = open(f).readlines(); l.reverse(); open(f, "w").writelines(l)' file1 file2 file3... ...and anyone who can read english would understand what is happening. so next ? but wait, here are some stats: [st...@laptop ~]$ cat <<EOF | wc -c perl -e 'undef$/;$"=chr(10);foreach(@ARGV){open(F,$_); $l=<F>;@l=reverse(split(/\n/,$l));close(F);open(F,">$_");print F "@l";close(F);}' EOF 129 [st...@laptop ~]$ cat <<EOF | wc -c python -c 'import sys for f in sys.argv[1:]: l = open(f).readlines(); l.reverse(); open(f, "w").writelines(l)' EOF 111 [st...@laptop ~]$ time perl -e 'undef$/;$"=chr(10);foreach(@ARGV){open(F,$_); $l=<F>;@l=reverse(split(/\n/,$l));close(F);open(F,">$_");print F "@l";close(F);}' bar real 0m0.112s user 0m0.050s sys 0m0.047s [st...@laptop ~]$ time python -c 'import sys > for f in sys.argv[1:]: l = open(f).readlines(); l.reverse(); open(f, "w").writelines(l)' bar real 0m0.073s user 0m0.043s sys 0m0.028s > [Don't try this in a directory where you have files valuable to you: it > reverses the lines in the files whose name you provide on the command > line. On the other hand, if you run it twice the files are back to > their original stage (I think :) ] > When I read this description, i immediately tried to write the equivalent in python. In my enthusiasm, I didn't bother to verify what your one-liner /actually/ does -- it reverses the order of the lines in the file. A cursory glance led me to believe it reversed every /character/ of every line in the file and I came up with: python -c "import sys for fl in sys.argv[1:]: rev = ''.join([ l[-1::-1] for l in open(f).readlines() ]); open(f, 'w').write(rev + '\n')" ...which is butt ugly, but did just that. > You'd be surprised how may of these semi-complex one-liners I've used to > get Real Work done, both personally and for clients (e.g. I remember > when the client wanted a quick list of all the IP addresses that had hit > his site more than 50 times from logs spread over a thousand files, and > the list of URLs they had hit). Yup, you got it -- Perl one-liner! > Sure, I believe that, though it'd be naive to assume that it won't be possible with python too. > I must admit there's a downside to this: if you use Perl you can't > charge the client a bomb for developing an application which will do the > same task in 1000 lines of beautifully indented and commented code -- > not because you can't write that in Perl, but because it seems such a > waste when one line is enough ;) > Well, that's really up to you. If I ever have to write the same one-liner more than say 5 times, I make a script out of it; and the scripting/programming language doesn't matter. cheers, - steve -- random non tech spiel: http://lonetwin.blogspot.com/ tech randomness: http://lonehacks.blogspot.com/ what i'm stumbling into: http://lonetwin.stumbleupon.com/ -- http://mm.glug-bom.org/mailman/listinfo/linuxers

