从文本文件中删除unicode字符 - sed,其他bash/shell方法

alv*_*vas 42 unicode bash spaces sed text-files

如何从终端上的一堆文本文件中删除unicode字符?我试过这个,但它不起作用:

sed 'g/\u'U+200E'//' -i *.txt
Run Code Online (Sandbox Code Playgroud)

我需要从文本文件中删除这些unicodes

U+0091 - sort of weird "control" space
U+0092 - same sort of weird "control" space
A0 - non-space break
U+200E - left to right mark
Run Code Online (Sandbox Code Playgroud)

kev*_*kev 59

清除所有非ascii字符 file.txt

$ iconv -c -f utf-8 -t ascii file.txt
$ strings file.txt
Run Code Online (Sandbox Code Playgroud)

  • 为什么你不能反过来运行它?tempf = $(mktemp)iconv -c -f utf-8 -t ascii file.txt> $ tempf iconv -f ascii -t utf-8 $ tempf> file.txt (2认同)
  • UTF-8 是 ASCII 的有效子集。反向转换保持文件不变。 (2认同)

Mic*_*jer 42

如果你想删除特定字符并且你有python,你可以:

CHARS=$(python -c 'print u"\u0091\u0092\u00a0\u200E".encode("utf8")')
sed 's/['"$CHARS"']//g' < /tmp/utf8_input.txt > /tmp/ascii_output.txt
Run Code Online (Sandbox Code Playgroud)

  • 它是python部分的替代代码.python -c'print"".join(map(unichr,range(0x80,0xa0)+ range(0x2000,0x200f))).encode("utf-8")'` (2认同)
  • 在最近的 linux 操作系统中,您可以通过按 Ctrl+Shift+u 后跟数字代码和 &lt;Enter&gt; 来编写 unicode 字符,例如 `Ctrl+Shift+u 0019 ⏎` (2认同)

cho*_*oba 27

对于unicode的utf-8编码,您可以将此正则表达式用于sed:

sed 's/\xc2\x91\|\xc2\x92\|\xc2\xa0\|\xe2\x80\x8e//g'
Run Code Online (Sandbox Code Playgroud)

  • `echo -ne'\ u0091'| xxd` (21认同)
  • 我如何找到从"U + ..."到"\ xc2\..."的映射? (2认同)
  • 这里系统之间存在相当多的差异。MacOS 不支持 \xNN 代码,RHEL 需要使用 -r 选项才能让 sed 使用它们。只是要记住一些事情,以防您在一个系统上开发脚本并部署到另一个系统(通常不是最好的主意,但这永远不会阻止人们这样做):) (2认同)

Mic*_*jer 13

使用iconv:

iconv -f utf8 -t ascii//TRANSLIT < /tmp/utf8_input.txt > /tmp/ascii_output.txt
Run Code Online (Sandbox Code Playgroud)

这会将"Š"等字符翻译成"S"(最相似的字符).