Last active
March 20, 2023 13:49
-
-
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
This file contains hidden or 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
#!/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