jemfinch
03-28-2001, 04:33 AM
So here's another tool. Because of a bug in python 2.0, you'll have to wait until 2.1 comes out (I developed this with the beta, as you can tell from the first line) for this to actually work.
I wrote this so I could stop using perl -[pn]i -e and grep so often.
#!/usr/experimental/bin/python2.1
import re
import sys
import string
import getopt
import fileinput
inplace = 0
regexps = []
# perldoc perlre for an explanation of "(?<!", the "negative lookbehind
# assertion." Basically, this is used to split on regexps that *aren't*
# escaped with a backslash.
splitre = re.compile(r'(?<!\\)/')
USAGE = """
-i : In-place file editing.
-m/regexp/flags : Print only lines that match regexp.
-M/regexp/flags : Print only lines that don't match regexp.
-s/findre/replacement/flags : Replace findre with replacement, print result.
flags can be any of the following:
i : ignore case in the regexp.
L : use locale settings for \[bBwW]
m : make ^ and $ apply to each \n separated line.
s : make . match all characters, include \n.
"""
def usage(e):
print '%s: %s' % (sys.argv[0], e)
print '%s -smMi <files>' % sys.argv[0], USAGE
sys.exit(-1)
try:
args, files = getopt.getopt(sys.argv[1:], 's:m:M:i')
except getopt.error, e:
usage(e)
for arg, value in args:
flag = 0
if arg == '-m':
regexp, flags = re.split(splitre, value)[1:]
for c in flags:
flag |= getattr(re, string.upper(c))
regexps.append(lambda s, r=regexp, f=flag: (re.search(r, s, f) and s)\
or '')
if arg == '-M':
regexp, flags = re.split(splitre, value)[1:]
for c in flags:
flag != getattr(re, string.upper(c))
regexps.append(lambda s, r=regexp, f=flag: ((not re.search(r, s, f))\
and s) or '')
if arg == '-s':
search, replace, flags = re.split(splitre, value)[1:]
for c in flags:
flag |= getattr(re, string.upper(c))
regexps.append(lambda s, se=search, r=replace, f=flag: re.sub(se, r,
s, f))
if arg == '-i':
inplace = 1
for line in fileinput.input(files, inplace):
for regexp in regexps:
line = regexp(line)
if line:
print line,
It's public domain, in case you like it.
Jeremy
I wrote this so I could stop using perl -[pn]i -e and grep so often.
#!/usr/experimental/bin/python2.1
import re
import sys
import string
import getopt
import fileinput
inplace = 0
regexps = []
# perldoc perlre for an explanation of "(?<!", the "negative lookbehind
# assertion." Basically, this is used to split on regexps that *aren't*
# escaped with a backslash.
splitre = re.compile(r'(?<!\\)/')
USAGE = """
-i : In-place file editing.
-m/regexp/flags : Print only lines that match regexp.
-M/regexp/flags : Print only lines that don't match regexp.
-s/findre/replacement/flags : Replace findre with replacement, print result.
flags can be any of the following:
i : ignore case in the regexp.
L : use locale settings for \[bBwW]
m : make ^ and $ apply to each \n separated line.
s : make . match all characters, include \n.
"""
def usage(e):
print '%s: %s' % (sys.argv[0], e)
print '%s -smMi <files>' % sys.argv[0], USAGE
sys.exit(-1)
try:
args, files = getopt.getopt(sys.argv[1:], 's:m:M:i')
except getopt.error, e:
usage(e)
for arg, value in args:
flag = 0
if arg == '-m':
regexp, flags = re.split(splitre, value)[1:]
for c in flags:
flag |= getattr(re, string.upper(c))
regexps.append(lambda s, r=regexp, f=flag: (re.search(r, s, f) and s)\
or '')
if arg == '-M':
regexp, flags = re.split(splitre, value)[1:]
for c in flags:
flag != getattr(re, string.upper(c))
regexps.append(lambda s, r=regexp, f=flag: ((not re.search(r, s, f))\
and s) or '')
if arg == '-s':
search, replace, flags = re.split(splitre, value)[1:]
for c in flags:
flag |= getattr(re, string.upper(c))
regexps.append(lambda s, se=search, r=replace, f=flag: re.sub(se, r,
s, f))
if arg == '-i':
inplace = 1
for line in fileinput.input(files, inplace):
for regexp in regexps:
line = regexp(line)
if line:
print line,
It's public domain, in case you like it.
Jeremy