awk-like usage of Python

Tom Culliton culliton at clark.net
Sun Apr 9 18:55:34 CEST 2000


In article <8cq5os$8b9$1 at nnrp1.deja.com>,  <ejbaker_seekonk at my-deja.com> wrote:
>I'm a relative newbie to Python. But, I have extensive experience with
>awk (it's an old stand-by that I rely on over and over).
>
>Are there any library modules that make python behave more like awk?
>Lacking that, what's the best way to achieve line by line parsing
>and pattern matching with Python?

Here's an awk like base class written when I first started using
Python.  It still sees heavy use today as the base class for many
filters which summarize data from log files.  I've seen other more
elaborate ones but this has continued to be useful for me and others.

--------------------------------filter.py--------------------------------
import sys
import os

def perror(*elist) :
	for e in elist :
		sys.stderr.write(str(e) + " ")
	sys.stderr.write("\n")

class file_filter :

	def __init__(self, filenames) :
		self.filenames = filenames
		self.filename = "stdin"
		self.file = sys.stdin
		self.line = ""
		self.nr = 0
		self.fnr = 0

	def run(self) :
		self.nr = 0
		self.fnr = 0
		self.begin()
		if not self.filenames :
			self.filename = "stdin"
			self.file = sys.stdin
			self.do_file()
		else :
		    for self.filename in self.filenames :
			self.fnr = 0
			try :
				ext = os.path.splitext(self.filename)[1]
				if ext == ".Z":
					self.file = os.popen("zcat " + self.filename, "rb")
				elif ext == ".gz":
					self.file = os.popen("gzip -dc " + self.filename, "rb")
				elif ext == ".bz2":
					self.file = os.popen("bzip2 -dc " + self.filename, "rb")
				else:
					self.file = open(self.filename, "rb")
			except IOError, msg :
				perror("Cannot open:", self.filename, msg)
				continue
			self.do_file()
			self.file.close()
		self.end()

	def do_file(self) :
		while 1 :
			self.line = self.file.readline()
			if self.line == "" : break
			self.nr = self.nr + 1
			self.fnr = self.fnr + 1
			self.do_line()

	def do_line(self) :
		print self.line,

	def begin(self) :
		pass

	def end(self) :
		pass



More information about the Python-list mailing list