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.0007412408{main}( ).../index.php:0
20.07944262680Joomla\CMS\Application\SiteApplication->execute( ).../index.php:49
30.07944262680Joomla\CMS\Application\SiteApplication->doExecute( ).../CMSApplication.php:196
40.318711453744Joomla\CMS\Application\SiteApplication->dispatch( ).../SiteApplication.php:233
50.321211478344Joomla\CMS\Component\ComponentHelper::renderComponent( ).../SiteApplication.php:194
60.323511533672Joomla\CMS\Component\ComponentHelper::executeComponent( ).../ComponentHelper.php:377
70.324111561072require_once( '/var/www/vhosts/shan.info/httpdocs/components/com_k2/k2.php' ).../ComponentHelper.php:402
80.341011960704K2ControllerItem->execute( ).../k2.php:64
90.341011960704K2ControllerItem->display( ).../BaseController.php:710
100.357112611408K2ControllerItem->display( ).../item.php:78
110.357112611408K2ControllerItem->display( ).../controller.php:19
120.363712982448Joomla\CMS\Cache\Controller\ViewController->get( ).../BaseController.php:663
130.369113002816K2ViewItem->display( ).../ViewController.php:102
140.475815850920K2ViewItem->display( ).../view.html.php:742
150.475915850920K2ViewItem->loadTemplate( ).../HtmlView.php:230
160.478616023856include( '/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.0007412408{main}( ).../index.php:0
20.07944262680Joomla\CMS\Application\SiteApplication->execute( ).../index.php:49
30.07944262680Joomla\CMS\Application\SiteApplication->doExecute( ).../CMSApplication.php:196
40.318711453744Joomla\CMS\Application\SiteApplication->dispatch( ).../SiteApplication.php:233
50.321211478344Joomla\CMS\Component\ComponentHelper::renderComponent( ).../SiteApplication.php:194
60.323511533672Joomla\CMS\Component\ComponentHelper::executeComponent( ).../ComponentHelper.php:377
70.324111561072require_once( '/var/www/vhosts/shan.info/httpdocs/components/com_k2/k2.php' ).../ComponentHelper.php:402
80.341011960704K2ControllerItem->execute( ).../k2.php:64
90.341011960704K2ControllerItem->display( ).../BaseController.php:710
100.357112611408K2ControllerItem->display( ).../item.php:78
110.357112611408K2ControllerItem->display( ).../controller.php:19
120.363712982448Joomla\CMS\Cache\Controller\ViewController->get( ).../BaseController.php:663
130.369113002816K2ViewItem->display( ).../ViewController.php:102
140.475815850920K2ViewItem->display( ).../view.html.php:742
150.475915850920K2ViewItem->loadTemplate( ).../HtmlView.php:230
160.478616023856include( '/var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php' ).../HtmlView.php:701
back to top