Last active
October 6, 2015 18:18
-
-
Save henriquemoody/3034270 to your computer and use it in GitHub Desktop.
[content-finder] Find content into files.
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
#!/usr/bin/env bash | |
# Usage: {script} OPTIONS | |
# Find content into files. | |
# | |
# -h, --help Displays this help | |
# -a, --action Action to perform: "search", "files" or a sed pattern to | |
# apply to files (default "search") | |
# -d, --directory Directory to search (default ".") | |
# -e, --extension Extensions to search on (default "*") | |
# -x, --exclude Pattern to exclude | |
# | |
# Report bugs to Henrique Moody <henriquemoody@gmail.com> | |
# | |
declare ACTION='search' | |
declare DIRECTORY="." | |
declare EXTENSION="*" | |
declare EXCLUDE="" | |
_grep() | |
{ | |
egrep -Rn "${PATTERN}" \ | |
--color=auto \ | |
--include="${EXTENSION}" \ | |
"${DIRECTORY}" | |
} | |
_help() | |
{ | |
sed -E 's/^#\s?(.*)/\1/g' "${0}" | | |
sed -nE '/^Usage/,/^Report/p' | | |
sed "s/{script}/$(basename "${0}")/g" | |
} | |
_search() | |
{ | |
test -z "${EXCLUDE}" && | |
_grep || | |
_grep | | |
egrep -v "${EXCLUDE}" | |
} | |
_files() | |
{ | |
_search | cut -d : -f 1 | sort -u | |
} | |
_action() | |
{ | |
_files | | |
while read filename; do | |
sed -Ei "${ACTION}" "${filename}" | |
done | |
} | |
while [[ ! -z "${1}" ]]; do | |
case ${1} in | |
-h | --help) | |
_help | |
exit 0 | |
;; | |
-a | --action) | |
ACTION="${2}" | |
shift 2 | |
;; | |
-d | --directory) | |
DIRECTORY="${2}" | |
shift 2 | |
;; | |
-e | --extension) | |
EXTENSION="*.${2}" | |
shift 2 | |
;; | |
-x | --exclude) | |
EXCLUDE="($(echo "${2}" | sed 's/,/\|/g; s/(/\\(/g'))" | |
shift 2 | |
;; | |
-*) | |
echo "Unrecognized option \"${1}\"" 1>&2 | |
exit 2 | |
;; | |
*) | |
PATTERN="${1}" | |
shift 1 | |
;; | |
esac | |
done | |
if [[ ! -t 0 ]] && [[ -z "${PATTERN}" ]]; then | |
PATTERN=$(cat <&0) | |
fi | |
if [[ -z "${PATTERN}" ]]; then | |
_help 1>&2 | |
exit 1 | |
fi | |
case "${ACTION}" in | |
search) | |
_search | grep --color=auto "${PATTERN}" | |
;; | |
files) | |
_files | |
;; | |
*) | |
_action | |
;; | |
esac |
Monstro!!!
great
Bem trabalhado. Parabéns. Vou roubar a ideia do help reaproveitável :D
Obrigado, pessoal! :D
Nice man!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Foda :D