LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   how to grep for [string] (http://www.linuxquestions.org/questions/linux-general-1/how-to-grep-for-%5Bstring%5D-937483/)

spezticle 03-31-2012 08:23 PM

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.

syg00 03-31-2012 08:45 PM

Escape them with a backslash.

spezticle 03-31-2012 09:20 PM

Quote:

Originally Posted by syg00 (Post 4641725)
Escape them with a backslash.

ah perfect, i think :)

Code:

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

TommyC7 03-31-2012 10:08 PM

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

Code:

grep -r "\[music\]" *

spezticle 03-31-2012 11:10 PM

Quote:

Originally Posted by TommyC7 (Post 4641758)
You need the backslash before every bracket, including the closing bracket so more like:

Code:

grep -r "\[music\]" *

Thank you as well.

jaybutts 04-01-2012 09:00 AM

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 ~ |}>

colucix 04-01-2012 10:20 AM

Quote:

Originally Posted by jaybutts (Post 4641972)
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.

spezticle 04-02-2012 12:19 AM

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 :)


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