Created
March 30, 2013 00:57
-
-
Save anowell/5274763 to your computer and use it in GitHub Desktop.
Bash search function - Quickly search for files containing text Piping find output into grep and optionally into your editor
This file contains 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
# Function to search for string in a set of files | |
# | |
# Add this function to ~/.bashrc | |
# Restart terminal or run: source ~/.bashrc | |
# Optionally set EDITOR in ~/.bash_profile (I use "EDITOR=subl") | |
# | |
# Examples: | |
# # Outputs filenames and matches of all found occurrences of "authenticate_user" in .rb files | |
# search *.rb "authenticate_user" | |
# | |
# # Open .rb files containing "authenticate_user" in default editor | |
# # Uses EDITOR variable or falls back to 'open' (mac) or 'xdg-open' (linux) | |
# search *.rb "authenticate_user" -o | |
# | |
# # View a list of .rb files containing "authenticate_user" or "authenticate_admin" with 'less' | |
# search *.rb "authenticate_[user|admin]" -l | less | |
# | |
# # Open .rb files containing "authenticate_admin" in vim | |
# search *.rb "authenticate_admin" -x vim | |
# | |
# By design, the "options" are awkwardly at the end - it's by design to enable my iterative search workflow | |
# search *.scala Logger # too many results | |
# search *.scala LoggerFactory # better set of results | |
# search *.scala LoggerFactory -o # open results in default editor (e.g. sublime-text) | |
# Perhaps someday I'll make it more flexible with the options location. Perhaps. | |
function search() { | |
iname="$1" && shift | |
pattern="$1" && shift | |
args="$1" && shift | |
if [ "$args" = "-x" ]; then | |
xargs="$@" | |
fi | |
# Determine best editor for -o option | |
if [ -n "$EDITOR" ]; then | |
editor="$EDITOR" | |
elif which xdg-open &> /dev/null; then | |
editor="xdg-open" #linux | |
else | |
editor="open" # mac | |
fi | |
# echo -e "iname: $iname \npattern: $pattern \nargs:$args\nxargs:$xargs" | |
# echo -e "editor: $editor" | |
if [ "$iname" = "-h" ] || [ "$iname" = "--help" ] || [ -z "$iname" ] || [ -z "$pattern" ]; then | |
echo -e "Usage: search <filename_pattern> <grep_pattern> [option]" | |
echo -e "By default, prints list of files and matches" | |
echo -e "\t-l Output just list of files" | |
echo -e "\t-x <command> Pass the list of files into command via xargs" | |
echo -e "\t-o Open the list of files in default editor. Uses EDITOR variable or ." | |
elif [ "$args" = "-l" ]; then | |
find . -iname "$iname" -print0 | xargs -0 grep -l "$pattern" | |
elif [ "$args" = "-x" ]; then | |
find . -iname "$iname" -print0 | xargs -0 grep -l "$pattern" | xargs -o $xargs | |
elif [ "$args" = "-o" ]; then | |
find . -iname "$iname" -print0 | xargs -0 grep -l "$pattern" | xargs -o $editor | |
else | |
find . -iname "$iname" -print0 | xargs -0 grep --color -n "$pattern" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment