我正在测试我的python grepper脚本。它需要多种搜索模式,并且适用于正则表达式和纯文本。
对于此测试,我的input.txt包含以下两行:
foo blah is a bar
foo blah is bar
如果我跑:
cat input.txt | ./pgreper.py "foo %{0} is a %{1}"
我应该得到:foo blah是一个酒吧,作为输出,但我什么都没得到。
非常感谢这方面的任何帮助。
谢谢:)
ps请忽略我的评论。
#!/usr/bin/python3
import sys
import re
import time
import datetime
import inspect
import argparse
# The following section allows arguments to be passed to the module, eg input | ./pgreper.py pattern pattern --debug
parser = argparse.ArgumentParser(description='Python Grep.')
# Enable debugging, by adding --debug at the end of command
parser.add_argument('--debug', action='store_true', help='Print debug messages')
# nargs='+', enables multiple patterns to be entered at the commandline eg input | ./pgreper.py pattern pattern
parser.add_argument('pattern', type=str, nargs='+', help='Pattern(s) for pgrepping')
args = parser.parse_args()
# This is the class that allows for debugging a line in sys.stdin, if it matches all patterns.
class CodeTrace(object):
def __init__(self, line, pattern):
self.line = line
self.pattern = pattern
# @staticmethod
# This is the degugging method
def trace(self, line, pattern):
# Capture the current time, and format the timestamp, into a readable format
ts = datetime.datetime.fromtimestamp(time.time()).strftime('[%Y-%m-%d %H:%M:%S:%f]')
# Inspect allows us to blah, inspecting the stack allows us to retrieve information
stack = inspect.stack()
# Retrieve calling class information
the_class = stack[1][0].f_locals["self"].__class__
# Retrieves the calling method information
the_method = stack[1][0].f_code.co_name
# Retrieves the calling method's variables
the_variables = stack[1][0].f_code.co_varnames
# Formats the contents of the debug trace into a readable format,
# Any parameters passed to the method and the return value, are included in the debug trace
debug_trace = ("{} {}.{}.{} {} {} ".format(ts, str(the_class), the_method,the_variables, pattern, line))
# Send out the debug trace as a standard error output
sys.stderr.write(debug_trace + "\n")
# This is the class that does the pattern matching
class Grepper(object):
def __init__(self, patterns, debug=False):
# Every pattern that this module takes is compiled here, so that it may be searched for in sys.stdin
self.patterns = [re.compile(p) for p in patterns]
self.debug = debug
# This method compares the input, to the patterns compiled in Grepper
def matchline(self, debug):
for line in sys.stdin:
# This line compares all the patterns to the input, only if the input matches ALL patterns does it pass
if all(p.search(line) for p in self.patterns):
sys.stdout.write(line)
# this if statement runs the CodeTrace.trace function, if the user adds the --debug option in the cli
if self.debug:
CodeTrace(line, self.patterns).trace(line, args.pattern)
# This main function calls the grepper class and the matchline method, for the purpose of pattern matching
def main():
print(args.pattern)
Grepper(args.pattern, args.debug).matchline(args.debug)
# This allows the module to be used as a standalone module, or a reusable module in a different program
if __name__ == "__main__":
main()
答案 0 :(得分:1)
您当前正在将命令行参数解释为正则表达式模式,而不替换占位符%{0}
和%{1}
。您可能希望用.*
或类似的东西替换占位符,如下所示:
class Grepper(object):
def __init__(self, patterns, debug=False):
patterns= [re.sub(r'%\{\d\}', '.*', p) for p in patterns]
self.patterns = [re.compile(p) for p in patterns]