Last active
December 29, 2015 10:29
-
-
Save starlightsys/7657454 to your computer and use it in GitHub Desktop.
Quick script that ties together find and grep so you can search through file contents. Uses Python because I was already familiar with argparse.Much of the length and inelegance of the script is because I wanted to preserve colored output through the pipe.
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 argparse, subprocess | |
parser = argparse.ArgumentParser() | |
parser.add_argument("file_glob", help="a quoted string to use when globbing for files; example: \"*.py\"") | |
parser.add_argument("search_string", help="the string to search for in the files") | |
args = parser.parse_args() | |
find = subprocess.Popen(["find", ".", "-type", "f", "-name", args.file_glob, "-print0"], stdout=subprocess.PIPE) | |
grep = subprocess.Popen(["xargs", "-0", "grep", "-H", "-n", "--color", args.search_string], stdin=find.stdout) | |
# Use communicate() instead of sys.stdout.write() or the print statement, since | |
# print tacks on a newline at the end, and write() fails for some reason | |
grep.communicate()[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment