使用正则表达式查找bash不区分大小写 [英] bash find using regex is not case sensitive
问题描述
我需要找到以三个小写字母开头的文件,但是由于某种原因,我遇到了不希望的不区分大小写的行为.我正在使用带有-regex选项的find,但是它甚至可以找到以大写字母开头的文件.
I need to find files starting with three lowercase letters but for some reason I'm getting an undesired case-insensitive behavior. I'm using find with the -regex option but it finds even the files starting with capital.
$ find . -regextype posix-egrep -regex '.*/[a-z]{3}\w+\.abc'
./TTTxxx.abc
./tttyyy.abc
打印与以下内容相同:
$ find . -regextype posix-egrep -regex '.*/[A-Z]{3}\w+\.abc'
./TTTxxx.abc
./tttyyy.abc
如果我不是使用一个字符范围,而是使用一个字符,那么它会敏感,只打印小写文件.
If instead of using a range of characters I use a single character, works as sensitive, printing only the lowercase file.
find . -regextype posix-egrep -regex '.*/[t]{3}\w+\.abc'
./tttyyy.abc
我尝试使用不同的正则表达式,结果是相同的.
I've tried using different regextypes and the result is the same.
此外,一个egrep似乎可以正常工作:
In addition, an egrep to seems to work:
find . -regextype posix-egrep -regex '.*/.+\.abc' | egrep '/[a-z]\w+\.abc'
./tttyyy.abc
为什么使用char范围时"find -regex"不区分大小写?
Why is the "find -regex" case-insensitive when using a char range ?
注意:,我需要使用find命令,因为我需要使用-exec选项.
Note: I need to use find as I need the -exec option.
非常感谢.
推荐答案
Accroding to Why does [A-Z] match lowercase letters in bash?, the collation is the issue here:
带有语言环境(例如en_US)的标准归类具有以下顺序:
Standard collations with locales such as en_US have the following order:
aAbBcC ... xXyYzZ
a 和 z
(在 [a-z]
中)之间的所有大写字母,但 Z
除外.在 A
和 Z
之间(在 [AZ]
中)是所有小写字母,除了 a
.
Between a
and z
(in [a-z]
) are ALL uppercase letters, except for Z
.
Between A
and Z
(in [A-Z]
) are ALL lowercase letters, except for a
.
因此,您需要显式列出所有小写字母或更改排序规则: $ export LC_COLLATE = C
并使用标准的 [a-z]
.
So you need to list explicitly all lowercase letters or change the collation:
$ export LC_COLLATE=C
and use standard [a-z]
.
[...] {3} \ w \ .abc
-此模式,其中 [...]
是 [az]
或列出的小写字母,将为您提供文件名.
[...]{3}\w\.abc
- this pattern, where [...]
is [a-z]
or lowercase letters listed, will get you filenames.
这篇关于使用正则表达式查找bash不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!