我有一个名为"file1“的文本文件,其中包含以下数据:
apple
appLe
app^e
app\^e 现在给出的命令是:
1.)grep app[\^lL]e file1
2.)grep "app[\^lL]e" file1
3.)grep "app[l\^L]e" file1
4.)grep app[l\^L]e file1
output in 1st case : app^e
output in 2nd case :
apple
appLe
app^e
output in 3rd case :
apple
appLe
app^e
output in 4th case :
apple
appLe
app^e 为什么..?
请帮帮我!
发布于 2011-02-04 01:13:55
1.)grep app[\^lL]e file1shell会在grep看到转义()之前将其删除,因此这相当于app[^lL]e。括号中的位匹配任何不是(从^开始,因为它是第一个字符)l或l的值
2.)grep "app[\^lL]e" file1这一次,\转义^,以便与^、L或l匹配
3.)grep "app[l\^L]e" file1只有当集合是第一个字符时,^才会对其求反,因此它与^、L或l匹配
4.)grep app[l\^L]e file1^是转义的,但由于它不是第一个,所以没有任何区别,所以它与^、L或l匹配
发布于 2011-02-04 01:14:11
在第一种情况下,grep app[\^lL]e file1不是在命令行上引用模式,而是由shell负责其扩展。因此,搜索模式实际上变成了
app[^lL]e表示:"app",然后是除"L“或"l”之外的任何符号,然后是"e“。唯一符合的一行是
app^e在其他情况下,^要么进行转义并逐字匹配,要么位于模式的中间。
https://stackoverflow.com/questions/4889225
复制相似问题