Skip to content

Instantly share code, notes, and snippets.

@letsgetrandy
Last active December 17, 2015 05:49
Show Gist options
  • Save letsgetrandy/5560480 to your computer and use it in GitHub Desktop.
Save letsgetrandy/5560480 to your computer and use it in GitHub Desktop.
A handy way to search HTML files for any instances of a particular CSS class name.
#!/bin/bash
#
# Searches all HTML files and matches those where a
# particular CSS class name is used
if command -v ag >/dev/null 2>&1
then
# use silver-searcher if available
ag -G "\\.html$" "class=\"[^\"]*\\b$1\\b[^\"]*\""
elif command -v ack >/dev/null 2>&1
then
# else, use ack if available
ack --html "class=\"[^\"]*\\b$1\\b[^\"]*\""
else
# otherwise, just use grep
find . -name "*.html" | xargs grep --color=auto -nioP "class=\"[^\"]*\\b$1\\b[^\"]*\""
fi
@letsgetrandy
Copy link
Author

Use it from the bash command line to recursively search the current directory for html files in which the specified class name is found within a class="..." attribute.

cssgrep some-class-name

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