Short way to escape HTML in Bash?

后端 未结 5 1861
庸人自扰
庸人自扰 2020-12-08 06:24

The box has no Ruby/Python/Perl etc.

Only bash, sed, and awk.

A way is to replace chars by map, but it becomes tedious

5条回答
  •  心在旅途
    2020-12-08 07:26

    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'
    

提交回复
热议问题