sed with python

Mike Rovner mike at bindkey.com
Thu Sep 4 20:59:42 EDT 2003


"David Raleigh Arnold" <darnold4 at cox.net> wrote in message news:<pan.2003.08.28.11.30.17.760815 at cox.net>...
> 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




More information about the Python-list mailing list