top | item 32468584

(no title)

djsamseng | 3 years ago

Recursive grep: For every time you know you’ve written that code before but can’t remember the exact syntax. Filters out known build directories that would otherwise make it slow (moddify this to your personal use case).

https://github.com/djsamseng/cheat_sheet/blob/main/grep_for_...

#!/bin/bash

if [ $# -eq 0 ] then echo "Usage: ./grep_for_text.sh \"text to find\" /path/to/folder --include=*.{cpp,h}" exit fi

text=$1 location=$2

# Remove $1 and $2 to pass remaining arguments as $@ shift shift

result=$(grep -Ril "$text" "$location" \ $@ \ --exclude-dir=node_modules --exclude-dir=build --exclude-dir=env --exclude-dir=lib \ --exclude-dir=.data --exclude-dir=.git --exclude-dir=data --exclude-dir=include \ --exclude-dir=__pycache__ --exclude-dir=.cache --exclude-dir=docs \ --exclude-dir=share --exclude-dir=odas --exclude-dir=dependencies \ --exclude-dir=assets)

echo "$result"

discuss

order

burntsushi|3 years ago

Disclosure: I'm the author of ripgrep.

As a sibling comment mentioned, assuming you're .gitignore files exclude all of that stuff from your repo, you should be able to just run 'rg "text to find"' to replace all of that. And use 'rg "text to find" -tcpp' if you want to limit it to C++ files.

I had similar scripts for recursive grep like that too. ripgrep replaced all of them.

alanbernstein|3 years ago

I have something similar. This is the reason my organisation of source code is primarily by-language at the top level.