Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-09-2006, 04:17 AM   #1
Registered: Aug 2005
Distribution: Arch Linux
Posts: 291

Rep: Reputation: 48
Question 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 ?

Old 03-09-2006, 04:32 AM   #2
Senior Member
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware64 14.2 and current, SlackwareARM current
Posts: 1,609

Rep: Reputation: 131Reputation: 131
grep -e "array\[[0-9][0-9]\]" test.c
do what you want?
Old 03-09-2006, 10:51 AM   #3
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
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.
Old 03-21-2006, 04:04 AM   #4
Registered: Aug 2005
Distribution: Arch Linux
Posts: 291

Original Poster
Rep: Reputation: 48
Thanks a lot titopoquito.
@Muha: thanks for that tip '{2}'.
That was helpful..



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 01:22 PM
grep problem in extracting special characters imppayel Programming 8 12-02-2004 08:28 AM
What does rpm -qa |grep th* (as compared to rpm -qa |grep th) display? davidas Linux - Newbie 2 03-18-2004 02:35 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 04:11 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration