Using grep to search texts with single quote?

Surround your search string with double quotes:

grep "'type' => 'select'"

You cannot escape single quotes that appear within single quotes. As explained in the bash manual:

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.


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