我有一个名为"file1"的文本文件,其中包含以下数据:
apple
appLe
app^e
app\^e
Run Code Online (Sandbox Code Playgroud)
现在给出的命令是:
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
Run Code Online (Sandbox Code Playgroud)
为什么这样..?
请帮忙..!
1.)grep app[\^lL]e file1
Run Code Online (Sandbox Code Playgroud)
在grep看到它之前,shell会删除转义符(\),所以这相当于app[^lL]e.括号中的位与任何不匹配(来自^,因为它是第一个字符)L或l
2.)grep "app[\^lL]e" file1
Run Code Online (Sandbox Code Playgroud)
这次,\逃脱^所以它匹配^或L或l
3.)grep "app[l\^L]e" file1
Run Code Online (Sandbox Code Playgroud)
^只有当它是第一个字符时才能使该集合无效,因此匹配^或L或l
4.)grep app[l\^L]e file1
Run Code Online (Sandbox Code Playgroud)
^被转义,但由于它不是第一个它没有任何区别,所以它匹配^或L或l