-   Linux - Newbie (
-   -   escapeing the characters '[' and ']' in grep (

logicalfuzz 03-09-2006 03:17 AM

escapeing the characters '[' and ']' in grep
i have used the grep command very effectively in the past. But right now i am facing a problem..
I want to find this string 'array[25]' in a text file.
i used:
grep 'array[[0-9]+]' test.c
grep 'array\[[0-9]+\]' test.c
grep array[[0-9]+] test.c

all dont seem to work as it does not show proper results.
How do i escape these special characers '[' and ']' ?
or for that matter how do i make grep work for this case ?


titopoquito 03-09-2006 03:32 AM


grep -e "array\[[0-9][0-9]\]" test.c
do what you want?

muha 03-09-2006 09:51 AM

some other solutions:

grep 'array\[[0-9]\{2\}\]' file
fgrep 'array[25]' file
egrep 'array\[[0-9]+\]' file
grep 'array\[[0-9]\{1,\}\]' file

{2} specifies the number of repeats of [0-9]
fgrep matches literal strings so you can't use regexp.
egrep uses extended regular expressions. So you can use the +
+ means one or more and is analogous to \{1,\} which is a regular expression so we can use that in grep in the last example.

logicalfuzz 03-21-2006 03:04 AM

Thanks a lot titopoquito.
@Muha: thanks for that tip '{2}'.
That was helpful..

All times are GMT -5. The time now is 05:05 PM.