LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 03-31-2012, 08:23 PM   #1
spezticle
Member
 
Registered: May 2010
Distribution: Ubuntu 10.04
Posts: 30

Rep: Reputation: 0
how to grep for [string]


how do you use grep to search for something that has brackets in it?

i'm trying to find all files in a directory structure that contain: "[music]" but i only want results for that exact string. searching for "music" returns way too much.

Last edited by spezticle; 03-31-2012 at 08:28 PM.
 
Old 03-31-2012, 08:45 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,059

Rep: Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971
Escape them with a backslash.
 
1 members found this post helpful.
Old 03-31-2012, 09:20 PM   #3
spezticle
Member
 
Registered: May 2010
Distribution: Ubuntu 10.04
Posts: 30

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by syg00 View Post
Escape them with a backslash.
ah perfect, i think

Code:
grep -r "\[music]" *
seems to be working. thanks

Last edited by spezticle; 03-31-2012 at 11:09 PM.
 
Old 03-31-2012, 10:08 PM   #4
TommyC7
Member
 
Registered: Mar 2012
Distribution: Slackware, CentOS, OpenBSD, FreeBSD
Posts: 435

Rep: Reputation: Disabled
You need the backslash before every bracket, including the closing bracket so more like:

Code:
grep -r "\[music\]" *
 
2 members found this post helpful.
Old 03-31-2012, 11:10 PM   #5
spezticle
Member
 
Registered: May 2010
Distribution: Ubuntu 10.04
Posts: 30

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TommyC7 View Post
You need the backslash before every bracket, including the closing bracket so more like:

Code:
grep -r "\[music\]" *
Thank you as well.
 
Old 04-01-2012, 09:00 AM   #6
jaybutts
LQ Newbie
 
Registered: Apr 2012
Location: Detroit
Distribution: Centos, Gentoo, Debian, Ubuntu, FreeBSD
Posts: 5

Rep: Reputation: Disabled
You don't need a backslash just use quotes, Just confirmed:

touch blah
09:59:13 root in ~ |}> cat blah
09:59:17 root in ~ |}> echo "[blah]" >> blah
09:59:29 root in ~ |}> grep "[blah]" blah
[blah]
09:59:37 root in ~ |}>
 
Old 04-01-2012, 10:20 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by jaybutts View Post
You don't need a backslash just use quotes, Just confirmed:

touch blah
09:59:13 root in ~ |}> cat blah
09:59:17 root in ~ |}> echo "[blah]" >> blah
09:59:29 root in ~ |}> grep "[blah]" blah
[blah]
09:59:37 root in ~ |}>
Not true. Without escapes the square brackets are interpreted as a character list (that is one of b, l, a or h):
Code:
$ echo bbbbb | grep "[blah]"
bbbbb
$ echo ciao | grep "[blah]"
ciao
This is the issue from where the question originated.
 
Old 04-02-2012, 12:19 AM   #8
spezticle
Member
 
Registered: May 2010
Distribution: Ubuntu 10.04
Posts: 30

Original Poster
Rep: Reputation: 0
Yeah, here is an example scenario for my original situation. I was trying to find only the exact phase [music] inside of "file1.conf"
One problem is that the word "music" would appear thousands of times inbetween the original path of "~/sandbox/" and the actual file1.conf and also I didn't exactly know that it was file1.conf that I was looking for. there are hundreds of files that I needed to sift through to find "[music]"

below is a scenario I built to test
Code:
23:53:35_somename@somePCname:~/sandbox$ dir -lR
.:
total 16
drwxr-xr-x 2 somename somename 4096 2012-04-01 23:52 music1
drwxr-xr-x 2 somename somename 4096 2012-04-01 23:52 music2
-rw-r--r-- 1 somename somename   12 2012-04-01 23:53 music.file
drwxr-xr-x 2 somename somename 4096 2012-04-01 23:49 somedirectory

./music1:
total 0
-rw-r--r-- 1 somename somename 0 2012-04-01 23:52 music.file

./music2:
total 0
-rw-r--r-- 1 somename somename 0 2012-04-01 23:52 music2.file

./somedirectory:
total 4
-rw-r--r-- 1 somename somename 9 2012-04-01 23:44 file1.conf
-rw-r--r-- 1 somename somename 0 2012-04-01 23:49 file2.conf
-rw-r--r-- 1 somename somename 0 2012-04-01 23:49 file3.txt
-rw-r--r-- 1 somename somename 0 2012-04-01 23:49 file.sh
I know the target that i'm looking for is only going to hit in "~/sandbox/somedirectory/file1.conf"
but i don't know that in my real situation.
in my scenario i have placed similar text that will return if my search string is not correct.
Code:
  GNU nano 2.2.6                        File: somedirectory/file1.conf                                                        

[music]
music
[music
music]
the problems without using the backslashes are shown below:
my term window has color coding which makes it a lot more clear what is being returned but if you try yourself you should see that it finds anything with an m, u, s, i, or c is returned and it doesn't search at all for the [ or ]
Code:
00:02:26_somename@somePCname:~/sandbox$ dir -Rl | grep "[music]"
drwxr-xr-x 2 somename somename 4096 2012-04-01 23:52 music1
drwxr-xr-x 2 somename somename 4096 2012-04-01 23:52 music2
-rw-r--r-- 1 somename somename   12 2012-04-01 23:53 music.file
drwxr-xr-x 2 somename somename 4096 2012-04-01 23:49 somedirectory
./music1:
-rw-r--r-- 1 somename somename 0 2012-04-01 23:52 music.file
./music2:
-rw-r--r-- 1 somename somename 0 2012-04-01 23:52 music2.file
./somedirectory:
-rw-r--r-- 1 somename somename 9 2012-04-01 23:44 file1.conf
-rw-r--r-- 1 somename somename 0 2012-04-01 23:49 file2.conf
-rw-r--r-- 1 somename somename 0 2012-04-01 23:49 file3.txt
-rw-r--r-- 1 somename somename 0 2012-04-01 23:49 file.sh
same thing in this situation below,
you see here, that it does find [music] in the file, file1.conf which is what I am looking for, but it does so only by searching for the word music.
color coding will show that it doesn't consider the [ or ] and in my real situation it's going to return thousands more music results.
Code:
00:17:57_somename@somePCname:~/sandbox$ grep -R "[music]" *
music.file:sample data
somedirectory/file1.conf:[music]
somedirectory/file1.conf:music
somedirectory/file1.conf:[music
somedirectory/file1.conf:music]
I appreciate the extra thoughts in posts but so far, syg00's original suggestion and TommyC7's correction are the only one that actually return the needle in the haystack i'm looking for, as shown below:
Code:
23:57:16_somename@somePCname:~/sandbox$ grep -r "\[music\]" *
somedirectory/file1.conf:[music]
thanks again
 
  


Reply

Tags
grep


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Issue trying to grep a string, but keep a similar string Supp0rtLinux Linux - Software 7 10-04-2011 06:35 PM
grep not getting whole string bic Programming 4 08-23-2010 12:07 PM
[SOLVED] How to GREP string? tquang Linux - General 2 07-30-2010 12:51 AM
grep a particular string john83reuben Linux - Newbie 3 12-07-2009 09:34 AM
grep string with space (2 word string) casperdaghost Linux - Newbie 7 08-24-2009 02:11 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration