Last active
October 25, 2017 21:48
-
-
Save freejoe76/3374344 to your computer and use it in GitHub Desktop.
Search all files matching a pattern for a particular string
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
#!/bin/bash | |
# For the times when grep -r just doesn't cut it: Search all files matching a pattern for a particular string | |
# Example command (best when put in a folder that's on your path somewhere): | |
# searchall.bash -f *.php -s 'hoops' | |
# What arguments do we pass? | |
while [ "$1" != "" ]; do | |
case $1 in | |
-f | --file ) shift | |
file=$1 | |
;; | |
-s | --search ) shift | |
search=$1 | |
;; | |
-l | --less ) | |
LESS=1 | |
;; | |
esac | |
shift | |
done | |
find . -name "$file" -exec grep "$search" '{}' \; -print | |
# To search and replace across documents, do something like | |
# find . -name "index.html" -print0 | xargs -0 sed -i '' -e 's#http://assets.nydailynews#img src="//www.nydailynews#g' | |
find . -name "index.html" -exec sed -i '{}' -e 's#http://assets.nydailynews#img src="//www.nydailynews#g' \; -print |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment