How to search for non-ASCII characters with bash t

2020-05-20 02:34发布

I have a large text file that contains a few unicode characters that make LaTeX crash. How can I find non-ASCII characters in a file with sed, and the like in a Linux bash?

2条回答
等我变得足够好
2楼-- · 2020-05-20 02:43

Try this command:

grep -P '[^\x00-\x7f]' file
查看更多
Bombasti
3楼-- · 2020-05-20 02:49

Try:

nonascii() { LANG=C grep --color=always '[^ -~]\+'; }

Which can be used like:

printf 'ŨTF8\n' | nonascii

Within [] ^ means "not". So [^ -~] means characters not between space and ~. So excluding control chars, this matches non ASCII characters, and is a more portable though slightly less accurate version of [^\x00-\x7f] below. The \+ means 1 or more and will get multibye characters to have a color shown around the complete character(s), rather than interspersed in each byte, thus corrupting the multibyte sequence

查看更多
登录 后发表回答