The box has no Ruby/Python/Perl etc.
Only bash/sed/awk.
A way is to replace chars by map, but it becomes tedious.
Perhaps some built-in functionality i'm not aware of?
Join Stack Overflow to learn, share knowledge, and build your career.
The box has no Ruby/Python/Perl etc.
Only bash/sed/awk.
A way is to replace chars by map, but it becomes tedious.
Perhaps some built-in functionality i'm not aware of?
Escaping HTML really just involves replacing three characters: <, >, and &. For extra points, you can also replace " and '. So, it's not a long sed script:
sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g'
recode, perl, php, xmlsarlet and w3m (a web browser for crying out loud). The last answer recommends using Python3 which although installed by default (in Ubuntu at least) is overkill too.
– WinEunuuchs2Unix
Mar 26 '17 at 23:43
< to <), and the answers there are trying to cover the possibility of random other entity references like é and numeric character references like É, rather than minimally-escaped HTML. For many purposes that might be overengineering, but on Stack Overflow it can be hard to tell exactly what someone's purpose is, so I don't blame the answerers there for wanting to provide something universal.
– ruakh
Mar 26 '17 at 23:58
You can use recode utility:
echo 'He said: "Not sure that - 2<1"' | recode ascii..html
Output:
He said: "Not sure that - 2<1"
Can't comment yet, so here's a new answer:
The previous sed replacement defaces valid output like
<
into
&lt;
Adding a negative loook-ahead so "&" is only changed into "&" if that "&" isn't already followed by "amp;" fixes that:
sed 's/&(?!amp;)/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g'
&, it is because I want it to be rendered by some web browser as &. That is why it must be turned into &amp;. That way, HTML-encoding and HTML-decoding are in balance. You don't suppress HTML-encoding just because the input looks like it has already been HTML-encoded. HTML-encoding is not idempotent. Failure to grasp that, eventually leads to XSS vulnerabilities.
– Ruud Helderman
Nov 10 '15 at 20:47