Re: sed with python

U

U. N. Owen

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
 
D

David Raleigh Arnold

David Raleigh Arnold enlightened us with:

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
 
D

David Raleigh Arnold

David Raleigh Arnold enlightened us with:

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
 
D

David Raleigh Arnold

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
 
M

Mike Rovner

David Raleigh Arnold said:
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
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top