Skip to content

Instantly share code, notes, and snippets.

@NikolasK-source
Created September 1, 2023 06:29
Show Gist options
  • Save NikolasK-source/b45f017f4934e18d54e70c70cf6939a6 to your computer and use it in GitHub Desktop.
Save NikolasK-source/b45f017f4934e18d54e70c70cf6939a6 to your computer and use it in GitHub Desktop.
Wrapper to simplify recursive grep with a pearl regex expression in the current working directory
#!/usr/bin/python
#
# Wrapper to simplify a recursive grep with a pearl compatible regular expression
#
# MIT License
#
# Copyright (c) 2023 Nikolas Koesling <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import sys
import subprocess
__VERSION__ = "1.0.0"
def main():
# parse args
parser = argparse.ArgumentParser(
description="Recursive grep with a pearl regular expression")
parser.add_argument("--no-line-number", action="store_true",
help="Do not prefix each line of output with the line number within its input file")
parser.add_argument("-v", "--invert-match", action="store_true",
help="select non-matching lines")
parser.add_argument("-i", "--ignore-case", action="store_true",
help="Ignore case distinctions in patterns and input data")
parser.add_argument("--no-color", action="store_true", help="disable colored output")
parser.add_argument("--version", action="store_true", help="print version and exit")
parser.add_argument("--no-follow-links", action="store_true", help="don'f follow symbolic links")
parser.add_argument('pattern', type=str, help="search pattern (pearl compatible regex)")
parser.add_argument('path', type=str, help="search path (default: current working directory)",
nargs='?', default=".")
args = parser.parse_args()
if args.version:
print(f"rpgrep {__VERSION__}")
exit(0)
# create grep flags
flags = f"-{'r' if args.no_follow_links else 'R'}P"
if not args.no_line_number:
flags += "n"
if args.invert_match:
flags += "v"
if args.ignore_case:
flags += "i"
cmd = ["grep", flags]
# colorize if output is a tty and no-color is not set
if args.no_color:
cmd.append("--color=never")
elif sys.stdout.isatty():
cmd.append("--color=always")
else:
cmd.append("--color=auto")
cmd.append(args.pattern)
cmd.append(args.path)
# execute command
x = subprocess.call(cmd)
exit(x)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment