Skip to content

Instantly share code, notes, and snippets.

@chrisshroba
Last active December 16, 2016 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisshroba/c57bf59f11e1dd9f720b364429bae63f to your computer and use it in GitHub Desktop.
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)
#!/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