Skip to content

Instantly share code, notes, and snippets.

@ruudud
Created October 28, 2013 09:42
Show Gist options
  • Save ruudud/7193993 to your computer and use it in GitHub Desktop.
Save ruudud/7193993 to your computer and use it in GitHub Desktop.
Replace special unicode characters (e.g. ❤) with code points in ASCII (e.g. \2764). Written with CSS in mind.
#!/usr/bin/env bash
set -e
# Find unicode chars outside ASCII range recursively from this directory
chars=$(grep -r -P -n "[\x80-\xFF]" . | awk -F':' '{print $1 ";" $2}')
for fileandline in $chars; do
arrIN=(${fileandline//;/ })
filename=${arrIN[0]}
linenumber=${arrIN[1]}
# Print the whole line
line=$(sed -n ${linenumber}p ${filename})
# Get only the special character
char=$(echo -en $line | sed -e 's/.*"\(.\)".*/\1/')
# Use Python to convert to unicode hex
unihex=$(python -c "print '%02X' % ord('$char'.decode('utf-8'))")
echo -e "${filename}: ${char} → \\${unihex}"
sed -i -e 's|'"$char"'|\\'"$unihex"'|' $filename
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment