grep 正则表达式选项要记得转义

使用过程中,使用最多的参数就是 -v ,但是用着并不爽。

比如说,我想查找一个单词“UserService”,但是像”*.svn” 这种文件就不用显示了,我该怎么做呢?

grep -r "UserService" ./ | grep -v "svn"

但是,如果类似于含有”test、auto_load”之类的文件我也不显示,怎么做呢?我之前的做法是:

grep -r "UserService" ./ | grep -v "svn" | grep -v "test" | grep -v "auto_load"

命令很长,而且麻烦,于是就想,grep本身是按照正则表达式来当做选项的,那么我是不是可以利用到正则表达式的“或|”命令?

grep -r "UserService" ./ | grep -v "svn|test|auto_load"

很显示,执行结果显示上面的命令不符合我的需求,于是苦思不得其解。原来,在使用正则表达式选项时,要记得将”|”转义。最终命令如下:

grep -r "UserService" ./ | grep -v "svn\|prj\|test\|auto_load"

( ! ) Warning: count(): Parameter must be an array or an object that implements Countable in /var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php on line 169
Call Stack
#TimeMemoryFunctionLocation
10.0004414200{main}( ).../index.php:0
20.08774725504Joomla\CMS\Application\SiteApplication->execute( ).../index.php:49
30.08774725504Joomla\CMS\Application\SiteApplication->doExecute( ).../CMSApplication.php:196
40.264212467072Joomla\CMS\Application\SiteApplication->dispatch( ).../SiteApplication.php:233
50.264712491752Joomla\CMS\Component\ComponentHelper::renderComponent( ).../SiteApplication.php:194
60.265312547080Joomla\CMS\Component\ComponentHelper::executeComponent( ).../ComponentHelper.php:377
70.265612575904require_once( '/var/www/vhosts/shan.info/httpdocs/components/com_k2/k2.php' ).../ComponentHelper.php:402
80.272313012576K2ControllerItem->execute( ).../k2.php:64
90.272313012576K2ControllerItem->display( ).../BaseController.php:710
100.280513738296K2ControllerItem->display( ).../item.php:78
110.280513738296K2ControllerItem->display( ).../controller.php:19
120.284214135224Joomla\CMS\Cache\Controller\ViewController->get( ).../BaseController.php:663
130.285014155592K2ViewItem->display( ).../ViewController.php:102
140.345317134088K2ViewItem->display( ).../view.html.php:742
150.345317134088K2ViewItem->loadTemplate( ).../HtmlView.php:230
160.346917314024include( '/var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php' ).../HtmlView.php:701

( ! ) Notice: Only variables should be assigned by reference in /var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php on line 478
Call Stack
#TimeMemoryFunctionLocation
10.0004414200{main}( ).../index.php:0
20.08774725504Joomla\CMS\Application\SiteApplication->execute( ).../index.php:49
30.08774725504Joomla\CMS\Application\SiteApplication->doExecute( ).../CMSApplication.php:196
40.264212467072Joomla\CMS\Application\SiteApplication->dispatch( ).../SiteApplication.php:233
50.264712491752Joomla\CMS\Component\ComponentHelper::renderComponent( ).../SiteApplication.php:194
60.265312547080Joomla\CMS\Component\ComponentHelper::executeComponent( ).../ComponentHelper.php:377
70.265612575904require_once( '/var/www/vhosts/shan.info/httpdocs/components/com_k2/k2.php' ).../ComponentHelper.php:402
80.272313012576K2ControllerItem->execute( ).../k2.php:64
90.272313012576K2ControllerItem->display( ).../BaseController.php:710
100.280513738296K2ControllerItem->display( ).../item.php:78
110.280513738296K2ControllerItem->display( ).../controller.php:19
120.284214135224Joomla\CMS\Cache\Controller\ViewController->get( ).../BaseController.php:663
130.285014155592K2ViewItem->display( ).../ViewController.php:102
140.345317134088K2ViewItem->display( ).../view.html.php:742
150.345317134088K2ViewItem->loadTemplate( ).../HtmlView.php:230
160.346917314024include( '/var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php' ).../HtmlView.php:701
back to top