command-line one-liners a la Perl?
unayok at gmail.com
Thu Jun 18 16:32:28 CEST 2009
On Jun 18, 9:36 am, kj <no.em... at please.post> wrote:
> I'm a recovering Perl addict, and I'm jonesin' badly for command-line
> one-liners, like
> % perl -lne '@f=split "\t";print join "\t", at f[3,1] if $f=~/frobozz/i' in.txt
> How can I get my fix with Python?
I'd encourage you to learn the ways of Python which ordinarily don't
encourage cramming lots of code into a single line.
However... if you are insistent on seeing how much rope Python can
to hang yourself... ;)
$ python -c 'print "\n".join( "\t".join( ( data, data ) ) for
data in ( lambda fn : ( line.strip().split("\t") for line in file( fn,
"r" ) ) )( "in.txt" ) if data.lower().find( "frobozz" ) > -1 )'
(untested) should do something similar to your perl statement (display
fourth and second fields from tab-delimited lines containing "frobozz"
any case-mixture in the third field for lines read from the "in.txt"
Note that the frobozz search is not a regex search in this example.
regex would make things more complicated.
Probably could be cleaned up a bit.
More information about the Python-list