如何为带有[]等特殊字符的字符串设置grep? [英] How do I grep for strings with special characters like []?
问题描述
我正在尝试查找所有包含文本的文件:$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']
,我尝试使用grep -rl '$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']' .
,但是后来发现[]
作为搜索模式具有特殊含义.那么搜索文本$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']
的正确命令是什么?
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']
?
推荐答案
您正确地认为[
和]
是特殊字符.用\
引用它们,或使用fgrep
代替.后者是纯字符串搜索:
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']" ...
尽管您还是要引用$
,因为它会由bash
和其他shell解释.
You still need to quote $
though because it is otherwise interpreted by bash
and other shells.
这篇关于如何为带有[]等特殊字符的字符串设置grep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!