Last active
December 16, 2016 14:41
-
-
Save chrisshroba/c57bf59f11e1dd9f720b364429bae63f to your computer and use it in GitHub Desktop.
A CLI for searching for a matching group and returning only that group (Rather than the entire matching line as grep does)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re | |
from sys import argv | |
if len(argv)<3: | |
print("Usage:") | |
print("{} <string> <pattern>".format(argv[0])) | |
exit(2) | |
s = argv[1] | |
pattern = argv[2] | |
lines = s.split('\n') | |
for line in lines: | |
match = re.search(pattern, line) | |
if match is not None: | |
result = match.groups()[0] | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment