Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Simple grep in Python
- ```python
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """Various text processing utilities.
- Majority of these are intended for working with files but will work on any
- string iterables as well.
- """
- import re
- def has_line(items, line):
- """Check if a line exists."""
- if not line.endswith('\n'):
- match = line + '\n'
- else:
- match = line
- return match in items
- def has_pattern(items, pattern, ignore_case=False):
- """Check if an item matching pattern exists."""
- regexp = re.compile(pattern, flags=ignore_case and re.I)
- for item in items:
- match = regexp.search(item)
- if match:
- return True
- else:
- return False
- def grep(items, pattern, ignore_case=False):
- """Get a list of all items matching regular expression."""
- regexp = re.compile(pattern, flags=ignore_case and re.I)
- matches = []
- for item in items:
- match = regexp.search(item)
- if match:
- matches.append(match.string)
- return matches
- def igrep(items, pattern, ignore_case=False):
- """Iterate over all items matching regular expression."""
- regexp = re.compile(pattern, flags=ignore_case and re.I)
- return filter(regexp.search, items)
- def cli(in_file, pattern, ignore_case=False, *args, **kwargs):
- """CLI entrypoint for `igrep`."""
- matches = igrep(in_file, pattern, ignore_case)
- # sort of a hack to check if iterator has at least one item
- try:
- first = next(matches)
- print(first, end='')
- except StopIteration:
- return 1
- for match in matches:
- print(match, end='')
- return 0
- if __name__ == '__main__':
- import argparse
- import sys
- parser = argparse.ArgumentParser()
- parser.add_argument(
- 'pattern',
- metavar='PATTERN',
- help='pattern to search for')
- parser.add_argument(
- 'in_file',
- nargs='?',
- metavar='FILE',
- default=sys.stdin,
- type=argparse.FileType('r'),
- help='file to search in')
- parser.add_argument(
- '-i',
- '--ignore-case',
- action='store_true',
- help='case insensitive match')
- args = parser.parse_args()
- sys.exit(cli(**vars(args)))
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement