如何为带有[]等特殊字符的字符串设置grep? [英] How do I grep for strings with special characters like []?

查看:251
本文介绍了如何为带有[]等特殊字符的字符串设置grep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找所有包含文本的文件:$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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆