In [ ]:
import re
from argparse import ArgumentParser
In [ ]:
ap = ArgumentParser()
ap.add_argument("pattern")
args = ap.parse_args(["^[a-z]+"])
print args
In [ ]:
pattern = re.compile(args.pattern)
In [ ]:
lines = """
foo
123
abc
*!%&
"""
for line in lines.split():
match = pattern.search(line)
if match:
print line
In [ ]:
ap.add_argument("files", nargs="+")
args = ap.parse_args(["^[a-z]+", "/etc/hosts", "/etc/pass*"])
print args
In [ ]:
import glob
pattern = re.compile(args.pattern)
for file in args.files:
file_list = glob.glob(file)
for file_name in file_list:
with open(file_name) as f:
for line in f.readlines():
match = pattern.search(line)
if match:
print "{}: {}".format(file_name, line.strip())
In [ ]:
import fileinput
from itertools import chain
file_list = list(chain(*(glob.glob(f) for f in args.files)))
for line in fileinput.input(file_list):
match = pattern.search(line)
if match:
print "{}[{}]: {}".format(fileinput.filename(), fileinput.filelineno(), line.strip())