Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I grep for strings with special characters like []?

Tags:

linux

grep

I am trying to find all the files has text: $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'], I tried to use grep -rl '$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']' ., but later on found [] has special meaning as search pattern. So what is the right command for searching text $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']?

like image 677
user2507818 Avatar asked Aug 14 '13 09:08

user2507818


People also ask

How do you grep words with special characters?

If you include special characters in patterns typed on the command line, escape them by enclosing them in single quotation marks to prevent inadvertent misinterpretation by the shell or command interpreter. To match a character that is special to grep –E, put a backslash ( \ ) in front of the character.

How do you grep symbols?

Whenever you use a grep regular expression at the command prompt, surround it with quotes, or escape metacharacters (such as & ! . * $ ? and \ ) with a backslash ( \ ). finds any line in the file list starting with "b." displays any line in list where "b" is the only character on the line.

Can you use wildcards with grep?

The wildcard * (asterisk) can be a substitute for any number of letters, numbers, or characters. Note that the asterisk (*) works differently in grep. In grep the asterisk only matches multiples of the preceding character. The wildcard * can be a substitute for any number of letters, numbers, or characters.

Can I use * in grep command?

Grep can identify the text lines in it and decide further to apply different actions which include recursive function or inverse the search and display the line number as output etc. Special characters are the regular expressions used in commands to perform several actions like #, %, *, &, $, @, etc.


1 Answers

You are right that [ and ] are special characters. Quote them with \ or use fgrep instead. The latter is plain string search:

fgrep "\$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']" ...

You still need to quote $ though because it is otherwise interpreted by bash and other shells.

like image 90
Maxim Egorushkin Avatar answered Oct 08 '22 18:10

Maxim Egorushkin