Skip to content

Instantly share code, notes, and snippets.

@mrsarm
Last active January 31, 2022 19:11
Show Gist options
  • Save mrsarm/90631bbc3ca443a225e00a02bf42ef50 to your computer and use it in GitHub Desktop.
Save mrsarm/90631bbc3ca443a225e00a02bf42ef50 to your computer and use it in GitHub Desktop.
find-css: Bash script to find one or two words (CSS markups) across CSS, SCSS or LESS files
#!/usr/bin/env bash
#
# find-css: Find one or two words (CSS markups) across CSS, SCSS or LESS files
#
if [ "$#" == 0 -o "$1" == "-h" -o "$1" == "--help" ]
then
cat >&2 <<-'EOF'
find-css: Find one or two words (CSS markups) across CSS, SCSS or LESS files
in the folder passed as first parameter.
Both expressions (literal or regex expressions) can be away each other
up to 4 lines, or `-C LINES` (the `-C` parameter is optional and
must be the last one).
Also temp folders are omitted in the search.
Usage: find-css LOCATION EXPR1 [EXPR2] [-C NUMLINES]
Usage example:
$ find-css . "#header" ".menu" -C 8
EOF
exit -1
fi
LOCATION="$1"
EXPR1="$2"
EXPR2="$3"
if [ "$4" == "-C" ]
then
LINES="$5"
elif [ "$3" == "-C" ]
then
LINES="$4"
EXPR2=""
else
LINES="4"
fi
# To skip minified markup files, lines content are taken into account up
# to this number of chars, including the filepath output at the beginning,
# so extra ~40 chars is recommended
LINES_MAX_LONG=200
find "$LOCATION" -regextype posix-egrep -regex ".*\.(le|sc|c)ss$" \
! -path '*/node_modules/*' ! -path '*/.git/*' ! -path '*/.terraform/*' \
! -path '*/build/*' ! -path '*/target/*' ! -path '*/dist/*' ! -path '*/lib/*' \
! -path '*/temp/*' ! -path '*/tmp/*' \
! -path '*/.venv*/*' ! -path '*/.env*/*' ! -path '*/venv*/*' ! -path '*/env*/*' \
! -path '*/__pycache__/*' ! -path '*/*.egg-info/*' \
-exec egrep -Hn --color -C "$LINES" "$EXPR1" {} \; \
| cut -c 1-$LINES_MAX_LONG \
| egrep --color -C "$LINES" "$EXPR2" \
| egrep --color -C "$LINES" "($EXPR1|$EXPR2)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment