Skip to content

Instantly share code, notes, and snippets.

@Boldewyn
Last active March 20, 2023 13:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Boldewyn/4473790 to your computer and use it in GitHub Desktop.
Save Boldewyn/4473790 to your computer and use it in GitHub Desktop.
cssgrep prints the part of HTML files, that match a given CSS selector. Solution based on this answer on StackOverflow: http://stackoverflow.com/a/14187086/113195
#!/bin/bash
function _usage() {
cat <<USAGE
usage: $(basename $0) PATTERN [FILES]
Print the HTML that matches the CSS selector PATTERN.
USAGE
}
PATTERN=$1
shift
FILES="$@"
if [[ $PATTERN == "-h" || $PATTERN == "--help" ]]; then
_usage
exit
fi
if [[ $# == 0 ]]; then
FILES=.
fi
for file in $(find $FILES -type f -name \*.html -or -name \*.htm)
do
# Ignore errors, write the results to standard output.
CONTENT=$(hxnormalize -l 240 -x "$file" 2>/dev/null | hxselect "$PATTERN")
if [[ $CONTENT != "" ]]; then
echo -e "\e[32m$file: \e[0m$CONTENT"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment