Skip to content

Instantly share code, notes, and snippets.

@henriquemoody
Last active October 6, 2015 18:18
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henriquemoody/3034270 to your computer and use it in GitHub Desktop.
Save henriquemoody/3034270 to your computer and use it in GitHub Desktop.
[content-finder] Find content into files.
#!/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
@henriquemoody
Copy link
Author

Find all files that matches "substr" into current directory.

content-finder "substr"

Find all files that matches "substr" into directory "/tmp".

content-finder "substr" -d /tmp

Find all files that matches "substr" into current directory only for "*.php" files.

content-finder "substr" -e php

Find all files that matches "substr" into current directory only for "*.php"
files and excludes lines that matches "tests" or "vendor".

content-finder -e php -x tests,vendor "substr"

Displays only the file names that matches "substr"

content-finder "substr" -a files

Search for *.php files that matches "print" and thant apply a sed command that
replaces "print" by "echo".

content-finder "\bprint\b" -e php -a 's/\bprint\b/echo/g'

You can create an alias, like

alias php-content-finder="content-finder -e php"

@nelsonsar
Copy link

Foda :D

@iamtchelo
Copy link

Monstro!!!

@NandoKstroNet
Copy link

great

@oliveiraev
Copy link

Bem trabalhado. Parabéns. Vou roubar a ideia do help reaproveitável :D

@henriquemoody
Copy link
Author

Obrigado, pessoal! :D

@jaredpelliott
Copy link

Nice man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment