Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am using gedit text editor with embedded terminal in ubuntu 12.04. I'm trying to search for some text using grep. I want to search for this line of code

'type' => 'select'

I tried:

grep -r '\'type\' => \'select\''

But grep didn't return any results.

So can someone kindly tell me how to search for the code above?

share|improve this question
add comment

3 Answers 3

Surround your search string with double quotes:

grep "'type' => 'select'"
share|improve this answer
add comment

cd to the directory that contains your .txt file

cd /path 

Then :

you can use grep "'type' => 'select'" name.txt

or :

`grep "'type' => 'select'" /path/file.txt

Output :

enter image description here

share|improve this answer
3  
The question only concerns the single quotes, not the directory structure or any of grep's other (slightly irrelevant) functionality. –  Chris Mar 10 at 6:09
add comment

You cannot escape single quotes that appear within single quotes. As explained here:

Enclosing characters in single quotes (‘'’) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

So, you have to use different approaches:

  1. Use double quotes:

    grep  "'type' => 'select'" file 
    
  2. If you prefer needlessly complex solutions:

    grep  "'"type"'"\ =\>\ "'"select"'" file 
    
  3. You can always search for any single character instead of specifying the single quotes:

    grep  '.type. => .select.' file 
    

But just use ", it makes things much more straightforward.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.