Replacing HTML ascii codes via a bash script?

2019-06-24 07:00发布

问题:

I need a way to replace HTML ASCII codes like ! with their correct character in bash.

Is there a utility I could run my output through to do this, or something along those lines?

回答1:

$ echo '!' | recode html/..
!
$ echo '<∞>' | recode html/..
<∞>


回答2:

Here is my solution with the standard Linux toolbox.

$ foo="This is a line feed&#010;And e acute:&#233; with a grinning face &#128512;."
$ echo "$foo"
This is a line feed&#010;And e acute:&#233; with a grinning face &#128512;.
$ eval "$(printf '%s' "$foo" | sed 's/^/printf "/;s/&#0*\([0-9]*\);/\$( [ \1 -lt 128 ] \&\& printf "\\\\$( printf \"%.3o\\201\" \1)" || \$(which printf) \\\\U\$( printf \"%.8x\" \1) )/g;s/$/\\n"/')" | sed "s/$(printf '\201')//g"
This is a line feed
And e acute:é with a grinning face               
                            
标签: bash ascii