command-line one-liners a la Perl?

unayok unayok at
Thu Jun 18 16:32:28 CEST 2009

On Jun 18, 9:36 am, kj <no.em... at> 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[2]=~/frobozz/i' in.txt
> How can I get my fix with Python?
> kynn

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
give you
to hang yourself... ;)

$ python -c 'print "\n".join( "\t".join( ( data[3], data[1] ) ) for
data in ( lambda fn : ( line.strip().split("\t") for line in file( fn,
"r" ) ) )( "in.txt" ) if data[2].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 mailing list