如何使用[]这样的特殊字符来搜索字符串?

时间:2023-01-13 22:06:24

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']?

我试图找到所有文件都有文本:$ GLOBALS ['TYPO3_CONF_VARS'] ['SC_OPTIONS'],我试图使用grep -rl'$ GLOBALS ['TYPO3_CONF_VARS'] ['SC_OPTIONS']'。,但稍后found []具有搜索模式的特殊含义。那么搜索文本$ GLOBALS ['TYPO3_CONF_VARS'] ['SC_OPTIONS']的正确命令是什么?

3 个解决方案

#1


13  

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

你是对的[和]是特殊字符。引用它们\或使用fgrep代替。后者是纯字符串搜索:

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

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

你仍然需要引用$,因为它是由bash和其他shell解释的。

#2


2  

You can use the -F or --fixed-strings to specify a list of strings to be matched.

您可以使用-F或--fixed-strings指定要匹配的字符串列表。

grep -Frl "\$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']" /path/to/files

grep -Frl“\ $ GLOBALS ['TYPO3_CONF_VARS'] ['SC_OPTIONS']”/ path / to / files

From the man page for grep:

从grep的手册页:

-F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX .)

-F, - fixed-strings将PATTERN解释为固定字符串列表,由换行符分隔,其中任何一个都要匹配。 (-F由POSIX指定。)

fgrep is deprecated but may be still available as an alternative to grep -F.

fgrep已被弃用,但仍可作为grep -F的替代品使用。

#3


1  

As Maxim Yegorushkin said, you have to escape the $ and [ ] characters.

正如马克西姆·叶戈鲁什金所说,你必须逃避$和[]字符。

grep -rl "\$GLOBALS\['TYPO3_CONF_VARS'\]\['SC_OPTIONS'\]" .

#1


13  

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

你是对的[和]是特殊字符。引用它们\或使用fgrep代替。后者是纯字符串搜索:

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

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

你仍然需要引用$,因为它是由bash和其他shell解释的。

#2


2  

You can use the -F or --fixed-strings to specify a list of strings to be matched.

您可以使用-F或--fixed-strings指定要匹配的字符串列表。

grep -Frl "\$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']" /path/to/files

grep -Frl“\ $ GLOBALS ['TYPO3_CONF_VARS'] ['SC_OPTIONS']”/ path / to / files

From the man page for grep:

从grep的手册页:

-F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX .)

-F, - fixed-strings将PATTERN解释为固定字符串列表,由换行符分隔,其中任何一个都要匹配。 (-F由POSIX指定。)

fgrep is deprecated but may be still available as an alternative to grep -F.

fgrep已被弃用,但仍可作为grep -F的替代品使用。

#3


1  

As Maxim Yegorushkin said, you have to escape the $ and [ ] characters.

正如马克西姆·叶戈鲁什金所说,你必须逃避$和[]字符。

grep -rl "\$GLOBALS\['TYPO3_CONF_VARS'\]\['SC_OPTIONS'\]" .