我需要解析包含哈希码值的字符串并将哈希码转换为等效的字符表示形式。这是它的示例代码。
I see that you#39;re eligible to get ticket for show on your device#44;
现在,脚本应该输出到
I see that you're eligible to get ticket for show on your device,
答案1
Perl 对此很有用:
$ str='I see that you#146;re eligible to get ticket for show on your device#44;'
$ perl -pe 's/#(\d+);/chr($1)/ge' <<<"$str"
I see that you’re eligible to get ticket for show on your device,
我必须将终端的编码设置为 WINDOWS-1252 才能获得该输出。十进制 146 无效ISO-8859-1 特点。
要将这些代码视为 HTML 实体,我们将添加缺少的 & 符号,然后进行解码:
perl -MHTML::Entities -lne 's/(#\d+;)/&$1/g; print decode_entities($_)' <<<"$str"
答案2
在 bash 中你可以这样做:
$ str='I see that you#39;re eligible to get ticket for show on your device#44;'
$ re='#([0-9]*);'; while [[ $str =~ $re ]]; do b="${BASH_REMATCH[1]}"; c=$(printf "$(printf '\\U%x' "$b")"); str=${str//#$b;/$c}; done; echo "$a"
I see that you're eligible to get ticket for show on your device,