command-line one-liners a la Perl?

K

kj

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",@f[3,1] if $f[2]=~/frobozz/i' in.txt

How can I get my fix with Python?

kynn
 
M

MRAB

kj said:
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",@f[3,1] if $f[2]=~/frobozz/i' in.txt

How can I get my fix with Python?
python -c "print 'Hello world!'"

Although you need to remember that Python makes much more use of
whitespace, which limits the usefulness of the command line.
 
U

unayok

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",@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
the
fourth and second fields from tab-delimited lines containing "frobozz"
in
any case-mixture in the third field for lines read from the "in.txt"
file).

Note that the frobozz search is not a regex search in this example.
Requiring
regex would make things more complicated.

Probably could be cleaned up a bit.

u.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top