我有几个非常大的XML文件,我试图找到包含非ascii字符的行。我试过以下几种方法:

grep -e "[\x{00FF}-\x{FFFF}]" file.xml

但是这将返回文件中的每一行,而不管该行是否包含指定范围内的字符。

是我的语法错误还是我做错了什么?我也试过:

egrep "[\x{00FF}-\x{FFFF}]" file.xml 

(在模式周围使用单引号和双引号)。


当前回答

如果你试图抓取/grep utf8兼容的多字节字符,使用这个:

(                     [\302-\337][\200-\277]|
                [\340][\240-\277][\200-\277]|
                [\355][\200-\237][\200-\277]|
  [\341-\354\356-\357][\200-\277][\200-\277]|
     [\360][\220-\277][\200-\277][\200-\277]|
[\361-\363][\200-\277][\200-\277][\200-\277]|
     [\364][\200-\217][\200-\277][\200-\277]  ) 

 * please delete all newlines, spaces, or tabs in between (..)

 * feel free to use bracket ranges {1,3} etc to optimize
   the redundant listings of [\200-\277]. but don't change that
   [\200-\277]+, as that might result in invalid encodings 
    due to either insufficient or too many continuation bytes

 * although some historical UTF-8 references considers 5- and 
   6-byte encodings to be valid, as of Unicode 13 they only
   consider up to 4-bytes

我甚至针对随机二进制文件测试了这个字符串,它将报告与gnu-wc相同的多字节字符计数。

添加另一个[\000-\177]|在前面(如果你需要完整的UTF8匹配字符串。

这个正则表达式确实很可怕,但它也符合posix,跨语言和跨平台兼容(不依赖于任何特殊的正则表达式符号,(应该)完全符合UTF-8 (Unicode 13),并且完全独立于语言环境设置。

如果你运行grep,请使用grep -P

如果您只需要其他字节,那么其他人已经建议过了。

如果你需要11,172个nfc组成的韩文

(([\352][\260-\277]|[\353\354][\200-\277]|
 [\355][\200-\235])[\200-\277]|[\355][\236][\200-\243])

如果你需要日本的平假名+片假名,它是

([\343]([\201-\203][\200-\277]|[\207][\260-\277]))

其他回答

您可以使用以下命令:

grep --color='auto' -P -n "[\x80-\xFF]" file.xml

这将为您提供行号,并将用红色突出显示非ascii字符。

在某些系统中,根据您的设置,上述方法将不起作用,因此您可以通过逆函数进行grep

grep --color='auto' -P -n "[^\x00-\x7F]" file.xml

还要注意,重要的位是-P标志,它相当于——Perl -regexp:因此它将把您的模式解释为Perl正则表达式。它还说

这是高度实验性的,grep -P可能会警告未实现 特性。

在perl中

perl -ane '{ if(m/[[:^ascii:]]/) { print  } }' fileName > newFile

下面的代码工作:

find /tmp | perl -ne 'print if /[^[:ascii:]]/'

将/tmp替换为要搜索的目录名。

最简单的方法是定义一个非ascii字符…作为非ASCII字符的字符。

LC_ALL=C grep '[^ -~]' file.xml

如果需要,在^后面添加一个制表符。

设置LC_COLLATE=C可以避免在许多地区中出现关于字符范围含义的令人讨厌的意外。必须设置LC_CTYPE=C以匹配单字节字符-否则该命令将错过当前编码中的无效字节序列。设置LC_ALL=C可以完全避免语言环境相关的影响。

如果你试图抓取/grep utf8兼容的多字节字符,使用这个:

(                     [\302-\337][\200-\277]|
                [\340][\240-\277][\200-\277]|
                [\355][\200-\237][\200-\277]|
  [\341-\354\356-\357][\200-\277][\200-\277]|
     [\360][\220-\277][\200-\277][\200-\277]|
[\361-\363][\200-\277][\200-\277][\200-\277]|
     [\364][\200-\217][\200-\277][\200-\277]  ) 

 * please delete all newlines, spaces, or tabs in between (..)

 * feel free to use bracket ranges {1,3} etc to optimize
   the redundant listings of [\200-\277]. but don't change that
   [\200-\277]+, as that might result in invalid encodings 
    due to either insufficient or too many continuation bytes

 * although some historical UTF-8 references considers 5- and 
   6-byte encodings to be valid, as of Unicode 13 they only
   consider up to 4-bytes

我甚至针对随机二进制文件测试了这个字符串,它将报告与gnu-wc相同的多字节字符计数。

添加另一个[\000-\177]|在前面(如果你需要完整的UTF8匹配字符串。

这个正则表达式确实很可怕,但它也符合posix,跨语言和跨平台兼容(不依赖于任何特殊的正则表达式符号,(应该)完全符合UTF-8 (Unicode 13),并且完全独立于语言环境设置。

如果你运行grep,请使用grep -P

如果您只需要其他字节,那么其他人已经建议过了。

如果你需要11,172个nfc组成的韩文

(([\352][\260-\277]|[\353\354][\200-\277]|
 [\355][\200-\235])[\200-\277]|[\355][\236][\200-\243])

如果你需要日本的平假名+片假名,它是

([\343]([\201-\203][\200-\277]|[\207][\260-\277]))