为什么'' grep [ff] irst *.txt''不匹配包含“首先”的文件?
孟德尔·库珀(Mendel Cooper)的“高级bash施法指南”中的示例...
$bash --version | head -1
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
$mkdir Quoting_Experimenting
$cd Quoting_Experimenting
$echo This is the first line of file1.txt > file1.txt
$echo This is the First line of file2.txt > file2.txt
$grep [fF]irst *.txt
file1.txt:This is the first line of file1.txt
file2.txt:This is the First line of file2.txt
$touch first
$grep [fF]irst *.txt
file1.txt:This is the first line of file1.txt
为什么GREP首次创建后找不到file2.txt?
Example from Mendel Cooper's "Advanced Bash-Scripting Guide"...
$bash --version | head -1
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
$mkdir Quoting_Experimenting
$cd Quoting_Experimenting
$echo This is the first line of file1.txt > file1.txt
$echo This is the First line of file2.txt > file2.txt
$grep [fF]irst *.txt
file1.txt:This is the first line of file1.txt
file2.txt:This is the First line of file2.txt
$touch first
$grep [fF]irst *.txt
file1.txt:This is the first line of file1.txt
Why does grep not find file2.txt after first has been created?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在grep命令中引用搜索模式,否则shell扩展了未引用的模式
[ff] irst
toholly first
仅(由于文件名首先
存在于当前目录中):请使用:
- 根据该文档的官方狂欢文件
(重点是我的):
You need to quote the search pattern in grep command otherwise shell expands unquoted pattern
[fF]irst
tofirst
only (due to filenamefirst
being present in your current directory):So use:
- Official BASH documentation on filename expansion
As per this doc (emphasis is mine):
如果您只是在寻找任何情况下的匹配项,而不是使用正则态度,只需使用ighore case命令:
If you're simply looking for a match with any case, instead of using a regex, just use the ignore case command: