I have a text file named “file1” containing the following data :
apple
appLe
app^e
app\^e
Now the commands given are :
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
why so..?
Please help..!
The escape (\) is removed by the shell before grep sees it so this is equivalent to
app[^lL]e. The bit in brackets matches anything not (from the ^, since it’s the first character) L or lThis time, the \ escapes the ^ so it matches ^ or L or l
^ works to negate the set only if it is the first character, so this matches ^ or L or l
The ^ is escaped, but since it’s not the first it doesn’t make any difference, so it matches ^ or L or l