Mar*_*rko 56 html bash html-escape-characters
我正在寻找一种方法来解决这个问题:
hello < world
Run Code Online (Sandbox Code Playgroud)
对此:
hello < world
Run Code Online (Sandbox Code Playgroud)
我可以使用sed,但如何在不使用神秘的正则表达式的情况下实现这一目标?
cev*_*ing 81
尝试重新编码(存档页面 ; GitHub镜像 ; Debian页面):
$ echo '<' |recode html..ascii
<
Run Code Online (Sandbox Code Playgroud)
在Linux和类似的Unix-y系统上安装:
$ sudo apt-get install recode
Run Code Online (Sandbox Code Playgroud)
在Mac OS上安装使用:
$ brew install recode
Run Code Online (Sandbox Code Playgroud)
小智 45
使用perl:
cat foo.html | perl -MHTML::Entities -pe 'decode_entities($_);'
Run Code Online (Sandbox Code Playgroud)
使用命令行中的php:
cat foo.html | php -r 'while(($line=fgets(STDIN)) !== FALSE) echo html_entity_decode($line, ENT_QUOTES|ENT_HTML401);'
Run Code Online (Sandbox Code Playgroud)
小智 16
使用xmlstarlet:
echo 'hello < world' | xmlstarlet unesc
Run Code Online (Sandbox Code Playgroud)
Win*_*nix 10
这个答案基于:在Bash中逃避HTML的简短方法?这适用于wget在Stack Exchange上获取答案(使用)并将HTML转换为常规ASCII字符:
sed 's/ / /g; s/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/#'/\'"'"'/g; s/“/\"/g; s/”/\"/g;'
Run Code Online (Sandbox Code Playgroud)
编辑1: 2017年4月7日 - 添加了左双引号和右双引号转换.这是bash脚本的一部分,网络擦除SE答案并将它们与本地代码文件进行比较:询问Ubuntu - 本地文件之间的代码版本控制和Ask Ubuntu答案
sed在Ask Ubuntu/Stack Exchange的1K行文件中使用~3秒将HTML转换为ASCII.因此我被迫使用Bash内置搜索并替换约1秒的响应时间.
这是功能:
#-------------------------------------------------------------------------------
LineOut="" # Make global
HTMLtoText () {
LineOut=$1 # Parm 1= Input line
# Replace external command: Line=$(sed 's/&/\&/g; s/</\</g;
# s/>/\>/g; s/"/\"/g; s/'/\'"'"'/g; s/“/\"/g;
# s/”/\"/g;' <<< "$Line") -- With faster builtin commands.
LineOut="${LineOut// / }"
LineOut="${LineOut//&/&}"
LineOut="${LineOut//</<}"
LineOut="${LineOut//>/>}"
LineOut="${LineOut//"/'"'}"
LineOut="${LineOut//'/"'"}"
LineOut="${LineOut//“/'"'}" # TODO: ASCII/ISO for opening quote
LineOut="${LineOut//”/'"'}" # TODO: ASCII/ISO for closing quote
} # HTMLtoText ()
Run Code Online (Sandbox Code Playgroud)
python 3.2+版本:
cat foo.html | python3 -c 'import html, sys; [print(html.unescape(l), end="") for l in sys.stdin]'
Run Code Online (Sandbox Code Playgroud)
在 macOS 上,您可以使用内置命令textutil(通常来说这是一个方便的实用程序):
echo '👋 hello < world 🌐' | textutil -convert txt -format html -stdin -stdout
Run Code Online (Sandbox Code Playgroud)
输出:
hello < world
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39126 次 |
| 最近记录: |