command-line one-liners a la Perl?

Discussion in 'Python' started by kj, Jun 18, 2009.

  1. kj

    kj Guest

    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
     
    kj, Jun 18, 2009
    #1
    1. Advertising

  2. kj

    MRAB Guest

    kj 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",@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.
     
    MRAB, Jun 18, 2009
    #2
    1. Advertising

  3. kj

    unayok Guest

    On Jun 18, 9:36 am, kj <> 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",@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.
     
    unayok, Jun 18, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tobi Reif
    Replies:
    5
    Views:
    109
    Tobi Reif
    Mar 13, 2006
  2. J. Romano
    Replies:
    18
    Views:
    247
    J. Romano
    Jul 13, 2004
  3. Larry
    Replies:
    1
    Views:
    117
    Martien Verbruggen
    Feb 3, 2005
  4. Replies:
    4
    Views:
    426
    John W. Krahn
    May 3, 2006
  5. Adam Funk
    Replies:
    9
    Views:
    211
    Adam Funk
    May 20, 2009
Loading...

Share This Page