LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 03-09-2006, 03:17 AM   #1
logicalfuzz
Member
 
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 ?

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

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

Rep: Reputation: 38
some other solutions:
Code:
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, 03:04 AM   #4
logicalfuzz
Member
 
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..
 
  


Reply

Tags
grep



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 12:22 PM
grep problem in extracting special characters imppayel Programming 8 12-02-2004 07:28 AM
What does rpm -qa |grep th* (as compared to rpm -qa |grep th) display? davidas Linux - Newbie 2 03-18-2004 01:35 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:24 AM.

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