Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batteries are included, so use them #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions patmch/patmch_v1.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import re, sys
import re
import sys
import operator
import itertools

if (len(sys.argv) < 2):
if len(sys.argv) != 1:
sys.stderr.write("Usage: patmatch.py in.pattern < file.in\n")
sys.exit(1)

# the following is not correct. we have to trim off the tailing '\n'
strip_newline = operator.itemgetter(slice(0,-1))

r = re.compile(sys.argv[1])
for line in sys.stdin:
if r.search(line[0:-1]):
sys.stdout.write(line)
def match(regex, lines_with_cr):
search = re.compile(regex).search
return itertools.ifilter(search, itertools.imap(strip_newline, lines_with_cr))

for line in match(sys.argv[1], sys.stdin):
print line