Skip to content

Instantly share code, notes, and snippets.

@fabiolimace
Last active February 3, 2025 04:38
Show Gist options
  • Save fabiolimace/286985e622ca632e64f439d09017a7b4 to your computer and use it in GitHub Desktop.
Save fabiolimace/286985e622ca632e64f439d09017a7b4 to your computer and use it in GitHub Desktop.
Convert HTML entities using AWK
# hexadecimal reference to decimal
echo "--ƒ--" \
| awk '{ while (match($0, /&#x[0-9a-fA-F]+;/)) { n = "0x" substr($0, RSTART + 3, RLENGTH - 4 ); x = sprintf("&#%d;", strtonum(n)); $0 = substr($0, 1, RSTART - 1) x substr($0, RSTART + RLENGTH); }; print }'
--ƒ--
# decimal reference to character
echo "--ƒ--" \
| awk '{ while (match($0, /&#[0-9]+;/)) { n = substr($0, RSTART + 2, RLENGTH - 3 ); sub(/^0+/, "", n); x = sprintf("%c", strtonum(n)); $0 = substr($0, 1, RSTART - 1) x substr($0, RSTART + RLENGTH); }; print }'
--ƒ--
# hexadecimal to decimal and decimal to character
echo "--ƒ--" \
| awk '{ while (match($0, /&#x[0-9a-fA-F]+;/)) { n = "0x" substr($0, RSTART + 3, RLENGTH - 4 ); x = sprintf("&#%d;", strtonum(n)); $0 = substr($0, 1, RSTART - 1) x substr($0, RSTART + RLENGTH); }; print }' \
| awk '{ while (match($0, /&#[0-9]+;/)) { n = substr($0, RSTART + 2, RLENGTH - 3 ); sub(/^0+/, "", n); x = sprintf("%c", strtonum(n)); $0 = substr($0, 1, RSTART - 1) x substr($0, RSTART + RLENGTH); }; print }'
--ƒ--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment