[英]How to split a file containing non-ascii characters into words, in bash?
[英]How to list all of the unique non-ascii characters in a file?
考虑这个README.md ,它包含许多非 ascii、unicode 字符。
我想使用 bash(最好在 OSX 上)提取所有独特的非 ascii 字符。
例如,我想作为输出:
²
³
½
×
–
‖
→
↔
∀
∂
∆
∈
≈
≥
️
🎞
🖥
目前,我有一个比较繁琐的命令,我想知道是否可以改进它:
LC_ALL=C cat README.md | sed -n "s/\(.\)/\1 /pg" | tr ' ' '\n' | grep '[^ -~]' | sort | uniq
sed 's/[\x00-\x7F]//g'
是我能想到的过滤掉 ASCII 字符的最简单方法。 您可以将其与grep -o.
每行一个字符。 而sort -u
相当于sort | uniq
sort | uniq
的。 这一切给出:
$ echo 2²🎞3³ | sed 's/[\x00-\x7F]//g' | grep -o . | sort -u
🎞
²
³
使用 grep 的变体:
grep -ohP '[^\x00-\x7F]' README.md | sort -u
在 shell 中使用 Python:
python3 -c "print('\n'.join(sorted(char for char in open('README.md').read() if ord(char) > 127)))"
为了不重复:
python3 -c "print('\n'.join(sorted({char for char in open('README.md').read() if ord(char) > 127})))"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.