Re: sed with python

Discussion in 'Python' started by U. N. Owen, Aug 27, 2003.

  1. U. N. Owen

    U. N. Owen Guest

    David Raleigh Arnold enlightened us with:
    > How do I use python as a wrapper for sed and/or gawk scripts?


    You don't. Python can do anything sed and awk can, and much, much
    more.

    Sybren



    That's true, but it may be useful to use
    existing code (and not bother rewriting in Python).
    You can use shell scripts, pipes, or if you
    want to use only python scripts, use
    os.system to call sed or awk.
    But if you write new functions, Python
    is enough, as Sybren says
    --
    _______________________________________________
    Get your free email from http://www.uymail.com

    Powered by Outblaze
     
    U. N. Owen, Aug 27, 2003
    #1
    1. Advertising

  2. On Wed, 27 Aug 2003 19:26:36 +0200, U. N. Owen wrote:

    >
    > David Raleigh Arnold enlightened us with:
    >> How do I use python as a wrapper for sed and/or gawk scripts?

    >
    > You don't. Python can do anything sed and awk can, and much, much more.
    >
    > Sybren
    >
    >
    >
    > That's true, but it may be useful to use existing code (and not bother
    > rewriting in Python). You can use shell scripts, pipes, or if you want to
    > use only python scripts, use
    > os.system to call sed or awk.
    > But if you write new functions, Python is enough, as Sybren says


    os.system it is. Thanks! Some of my stuff is simple substitution
    filters, and rewriting that makes no sense to me. DaveA

    --
    "We have learned the lessons of Viet Nam." --anon Spokesman
    "Failure is not an option." --anon Spokesman
    Can both be true? n Are both false? y Right answers get my vote.
    D. Raleigh Arnold dra@ http://www.openguitar.com
     
    David Raleigh Arnold, Aug 27, 2003
    #2
    1. Advertising

  3. On Wed, 27 Aug 2003 19:26:36 +0200, U. N. Owen wrote:

    >
    > David Raleigh Arnold enlightened us with:
    >> How do I use python as a wrapper for sed and/or gawk scripts?

    >
    > You don't. Python can do anything sed and awk can, and much, much more.
    >
    > Sybren
    >
    >
    >
    > That's true, but it may be useful to use existing code (and not bother
    > rewriting in Python). You can use shell scripts, pipes, or if you want to
    > use only python scripts, use
    > os.system to call sed or awk.
    > But if you write new functions, Python is enough, as Sybren says


    Thanks. I didn't realize at first that Sybren had not read
    the question before replying. I am using shell scripts, both
    very simple ones that call sed and more elaborate ones that
    extract fields from a database and do something with them.

    Obviously, os.system does me no good at all. If windows
    users had bash I would have no reason to use python to
    call it.

    Is there an equivalent to "tac" already written as a
    function in python? Is there a matrix transposition
    function that automagically adds fields to records so
    that all records have the same number of fields, to
    guard against data loss? Where are these found? DaveA

    --
    "We have learned the lessons of Viet Nam." --anon Spokesman
    "Failure is not an option." --anon Spokesman
    Can both be true? n Are both false? y Right answers get my vote.
    D. Raleigh Arnold dra@ http://www.openguitar.com
     
    David Raleigh Arnold, Aug 28, 2003
    #3
  4. On Thu, 28 Aug 2003 07:30:17 +0000, David Raleigh Arnold wrote:

    > Obviously, os.system does me no good at all.


    That was stupid of me. Of course it lets me use pipes and redirection,
    which mostly does all. DaveA

    > Is there an equivalent to "tac" already written as a function in python?
    > Is there a matrix transposition function that automagically adds fields to
    > records so that all records have the same number of fields, to guard
    > against data loss? Where are these found? DaveA


    --
    "We have learned the lessons of Viet Nam." --anon Spokesman
    "Failure is not an option." --anon Spokesman
    Can both be true? n Are both false? y Right answers get my vote.
    D. Raleigh Arnold dra@ http://www.openguitar.com
     
    David Raleigh Arnold, Aug 29, 2003
    #4
  5. U. N. Owen

    Mike Rovner Guest

    "David Raleigh Arnold" <> wrote in message news:<>...
    > On Wed, 27 Aug 2003 19:26:36 +0200, U. N. Owen wrote:
    >
    > >
    > > David Raleigh Arnold enlightened us with:
    > >> How do I use python as a wrapper for sed and/or gawk scripts?

    > >
    > > You don't. Python can do anything sed and awk can, and much, much more.

    >
    > Is there an equivalent to "tac" already written as a
    > function in python?


    It's trivial easy in latest (2.3) Python.
    If your filesize is relatively small and you don't use fancy options, than:

    import sys
    for name in sys.argv[1:]:
    inpfile = name=='-' and sys.stdin or file(name)
    lines=inpfile.readlines()
    sys.stdout.writelines(lines[::-1])

    HTH,
    Mike
     
    Mike Rovner, Sep 5, 2003
    #5
    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. Replies:
    5
    Views:
    788
  2. Raymond

    sed to python: replace Q

    Raymond, Apr 30, 2008, in forum: Python
    Replies:
    10
    Views:
    499
    Dan Stromberg
    Jun 5, 2008
  3. Mathieu Prevot
    Replies:
    2
    Views:
    396
    Mathieu Prevot
    Jul 7, 2008
  4. hofer
    Replies:
    11
    Views:
    2,650
  5. Daniel Fetchinson

    [OFF] sed equivalent of something easy in python

    Daniel Fetchinson, Oct 25, 2010, in forum: Python
    Replies:
    5
    Views:
    412
    Mark Wooding
    Oct 30, 2010
Loading...

Share This Page