grep 正则表达式用引号括起来和元字符加反斜杠转义的测试
grep 正则表达式用引号括起来和元字符加反斜杠转义的测试
实验在 grep 命令中的表达式:不加引号,加单引号,加双引号的区别,以及部分元字符前加与不加 `\’ 进行转义的区别。实验环境为“实验楼( http://www.shiyanlou.com/ )上的 CentOS 6 ,GNU grep 2.6.3。
1、测试不把 grep 的表达式加上引号:
[root@d9a69d7b11ac test]#mkdir test; cd test; touch hello
[root@d9a69d7b11ac test]# bash -x -c "ls ./* | grep hell*"
+ ls ./hello
+ grep hello
./hello
[root@d9a69d7b11ac test]# touch hello2
[root@d9a69d7b11ac test]# bash -x -c "ls ./* | grep hell*"
+ ls ./hello ./hello2
+ grep hello hello2
[root@d9a69d7b11ac test]# echo "hello3" > hello2
[root@d9a69d7b11ac test]# bash -x -c "ls ./* | grep hell*"
+ ls ./hello ./hello2
+ grep hello hello2
hello3
[root@d9a69d7b11ac test]#
可见,如果 grep 的表达式不加引号,碰到通配符等就会因为 bash 的扩展功能,而先把表达式进行扩展,扩展的结果再送入表达式进行grep 的命令执行。
2、
下面测试把 grep 的表达式加上单引号括起来的情况:
[root@d9a69d7b11ac test]# rm hello2
rm: remove regular empty file `hello2'? y
[root@d9a69d7b11ac test]# ls
hello
[root@d9a69d7b11ac test]# bash -x -c "ls ./* | grep 'hell*'"
+ grep 'hell*'
+ ls ./hello
./hello
[root@d9a69d7b11ac test]# bash -x -c "ls ./* | grep 'hell*'"
+ ls ./hello
+ grep 'hell*'
./hello
[root@d9a69d7b11ac test]# bash -x -c "ls ./* | grep 'hell*'"
+ grep 'hell*'
+ ls ./hello
./hello
[root@d9a69d7b11ac test]# touch hello2
[root@d9a69d7b11ac test]# bash -x -c "ls ./* | grep 'hell*'"
+ ls ./hello ./hello2
+ grep 'hell*'
./hello
./hello2
[root@d9a69d7b11ac test]# echo "hello3" > hello2
[root@d9a69d7b11ac test]# bash -x -c "ls ./* | grep 'hell*'"
+ ls ./hello ./hello2
+ grep 'hell*'
./hello
./hello2
[root@d9a69d7b11ac test]#
可见,grep 表达式用单引号括起来就避免了 bash 的预先扩展。
但是,要注意,bash -x 的展开功能的展开顺序不是固定的,如上,有时先展开 ls ./*,有时先展开 grep ‘hell*’(因为是加的单引号,所以展开后保持不变。)。但是,展开后的执行是先执行 ls ./* ,结果再传给 grep ‘hell*’ 执行的。
3、
下面测试把 grep 的表达式加双引号括起来的情况:
[root@d9a69d7b11ac test]# rm hello2
rm: remove regular empty file `hello2'? y
[root@d9a69d7b11ac test]# ls
hello
[root@d9a69d7b11ac test]# bash -x -c "ls ./* | grep "hell*""
+ ls ./hello
+ grep hello
./hello
[root@d9a69d7b11ac test]# bash -x -c 'ls ./* | grep "hell*"'
+ ls ./hello
+ grep 'hell*'
./hello
[root@d9a69d7b11ac test]# touch hello2
[root@d9a69d7b11ac test]# bash -x -c "ls ./* | grep "hell*""
+ ls ./hello ./hello2
+ grep hello hello2
[root@d9a69d7b11ac test]# bash -x -c 'ls ./* | grep "hell*"'
+ ls ./hello ./hello2
+ grep 'hell*'
./hello
./hello2
[root@d9a69d7b11ac test]# ls ./* | grep "hell*"
./hello
./hello2
[root@d9a69d7b11ac test]# echo "hello3" > hello2
[root@d9a69d7b11ac test]# bash -x -c "ls ./* | grep "hell*""
+ grep hello hello2
+ ls ./hello ./hello2
hello3
[root@d9a69d7b11ac test]# bash -x -c 'ls ./* | grep "hell*"'
+ ls ./hello ./hello2
+ grep 'hell*'
./hello
./hello2
[root@d9a69d7b11ac test]# ls ./* | grep "hell*"
./hello
./hello2
[root@d9a69d7b11ac test]# bash -x -c 'a="he"; ls ./* | grep "${a}ll*"'
+ a=he
+ ls ./hello ./hello2
+ grep 'hell*'
./hello
./hello2
[root@d9a69d7b11ac test]# bash -x -c 'a="he"; ls ./* | grep '${a}ll*''
+ a=he
+ ls ./hello ./hello2
+ grep 'll*'
./hello
./hello2
./hello2
[root@d9a69d7b11ac test]# a=he; ls ./* | grep '${a}ll*'
[root@d9a69d7b11ac test]# a=he; ls ./* | grep "${a}ll*"
./hello
./hello2
[root@d9a69d7b11ac test]# bash -x -c 'a="he"; ls ./* | grep ${a}ll*'
+ a=he
+ ls ./hello ./hello2
+ grep hello hello2
hello3
[root@d9a69d7b11ac test]# a="he"; ls ./* | grep ${a}ll*
hello3
[root@d9a69d7b11ac test]#
可以看出,grep 的表达式加上双引号,可以避免一部分 bash 扩展功能,如 ls ./* | grep “hell*” 中 grep 表达式中的 *;但是不能避免变量扩展,如可以扩展:a=”he”; ls ./* | grep “${a}ll*”。
另外,在测试 bash -x -c ‘a=”he”; ls ./* | grep ‘${a}ll*” 和 a=he; ls ./* | grep ‘${a}ll*’ 时,我们看见了不一样的结果,具体原因不明,有可能是 bash -c 的功能对’${a}ll*’作了一些修改?
另外,虽然暂时看来在 grep 中的表达式用双引号括起来似乎可以利用它扩展变量的功能把 grep 的正则表达式弄成变量,但是,这个功能有没有其它的副作用呢?目前没有查找到相关的文档依据,所以保险的做法是:在 grep 的正则表达式中只用单引号括起来。这种做法也是 grep 的 info 文档中的例子所采用的。
4、
实验一下 grep 使用基本正则表达式时,部分元字符必须加上转义 \ 的情况:
在 info grep 的菜单:* Regluar Expressions:: ->Basic vs Extended:: 中写道:
3.6 Basic vs Extended Regular Expressions=========================================
In basic regular expressions the meta-characters `?’, `+’, `{’, `|’,
`(’, and `)’ lose their special meaning; instead use the backslashed
versions `\?’, `\+’, `\{’, `\|’, `\(’, and `\)’.
所以,必须在 grep 的正则表达式中使用 \ 以使这些字符的作用生效。如下(继续使用上面的测试环境):
[root@e16578371323 test]# ls
hello hello2
[root@e16578371323 test]# ls ./* | grep 'hell?'
[root@e16578371323 test]# ls ./* | grep 'hell\?'
./hello
./hello2
[root@e16578371323 test]# ls ./* | grep 'hel{2}'
[root@e16578371323 test]# ls ./* | grep 'hel\{2\}'
./hello
./hello2
[root@e16578371323 test]#
5、
结论:在使用 grep 时,正则表达式一定要用单引号括起来,否则可能因为 shell 执行环境的预先展开功能导致错误;在基本正则表达式(grep 默认为基本正则表达式)中的元字符 `?’, `+’, `{’, `|’,`(’, `)’ 前面一定要加上 `\’ 进行转义。另外,注意区分通配符`?’, `*’, `[]‘ 与正则表达式中相应字符的含义和用法。
欢迎交流探讨,若有错漏,敬请批评与斧正。谢谢。
- 本文来自:Linux教程网
grep 正则表达式用引号括起来和元字符加反斜杠转义的测试的更多相关文章
- js进阶正则表达式7点数字字母空格(w d s)(小写表原意,大写表反义)(特殊字符要加反斜杠:var reg22=/\W/g)
js进阶正则表达式7点数字字母空格(w d s)(小写表原意,大写表反义)(特殊字符要加反斜杠:var reg22=/\W/g) 一.总结 1.w d s,word digital space 2.特 ...
- 正则表达式,字符串中需要两个反斜杠“\\d”
这个正则表达式为什么会有两个反斜杠? "^.*?\\.(jpg|png|bmp|gif)$"上面这个正则表达式为什么有两个反斜杠呢?反斜杠点\.就能表示点.了,为什么还要在\.前面 ...
- 每日linux命令学习-引用符号(反斜杠\,单引号'',双引号"")
引用符号在解析器中保护特殊元字符和参数扩展,其使用方法有3种:反斜杠(\),单引号(’‘),双引号(“”). 单引号和双引号必须匹配使用,均可在解析器中保护特殊元字符和通配符,但是单引号(硬转义)主要 ...
- python 正则表达式中反斜杠(\)的麻烦和陷阱
这里是一点小心得:由于下面两个原因,在正则表达式中使用反斜杠就会产生了一个双重转换的问题. (1).python自身处理字符串时,反斜杠是用于转义字符 (2).正则表达式也使用反斜杠来转义字符 ...
- 关于Python中正则表达式的反斜杠问题
之前总是搞不明白正则表达式中的反斜杠的问题.今天经过查阅资料终于搞明白了. 其中最重要的一点就是Python自己的字符串中定义的反斜杠也是转义字符,而正则表达式中的反斜杠也是转义字符,所以正则表达式中 ...
- Unix Shell中单引号、双引号字符、反斜杠、反引号的使用[转]
在执行shell脚本的时候,shell将会对脚本中的行进行解释,然后执行:对于一些特殊处理的句子,我们可以使用引号或者反斜线来避免shell解释执行之.如下,当在命令行中输入:echo *child. ...
- JS_正则表达式_使用字符串创建的正则表达式_反斜杠也需要添加转义符
备注: 使用字符串创建的正则表达式:"\"也需要加转义符: var reg1=new RegExp("\\w+"); 这和 直接使用:var r ...
- PHP在引号前面添加反斜杠的原因及PHP去除反斜杠的办法
昨天用PHP做了个读写html文档的小程序,本地测试正常但是传到网站后发现,提交内容保存的时候会自动在双引号前面增加一个反斜杠“\”,而且每保存一次增加一个反斜杠,很是郁闷. 当然做这个只是为了参加电 ...
- grep正则表达式后面的单引号和双引号的区别
单引号''是全引用,被单引号括起的内容不管是常量还是变量者不会发生替换:双引号""是部分引用,被双引号括起的内容常量还是常量,变量则会发生替换,替换成变量内容! 一般常量用单引号' ...
随机推荐
- web开发:jquery之DOM
一.文档结构 二.文档操作 三.文档操作案例 四.form表单 五.正则 六.form案例 一.文档结构 ```jsvar $sup = $('.sup');console.log($sup.chil ...
- 跟着我一步一步的搭建一个基于springcloud的微服务实例
Table of Contents generated with DocToc microservicecloud 插件推荐 建立父工程Microservicecloud 搭建Employ员工服务 创 ...
- 前端使用 node-gyp 构建 Native Addon
前端轮子千千万, 但还是有些瓶颈, 公司需要在前端调用自有 tcp 协议, 该协议只有 c++ 的封装版本. 领导希望可以直接调该模块, 不要重复造轮子. 实话说我对 C 还有点印象, 毕竟也是有二级 ...
- CF1037H Security——SAM+线段树合并
又是一道\(SAM\)维护\(endpos\)集合的题,我直接把CF700E的板子粘过来了QwQ 思路 如果我们有\([l,r]\)对应的\(SAM\),只需要在上面贪心就可以了.因为要求的是字典序比 ...
- k8s部署dashboard
1.首先去github上找到kubernetes 2.然后找到get started 3.复制yaml文件地址,并wget到服务器上并部署即可 PS:本文把自己部署的yaml文件贴出来:recomme ...
- P3254——DP&&入门
题目 给定一个$n \times m$的$01$矩形,选择其中为$1$的位置,要求互不相邻,问方案数. 解决方案 直接dp因为状态较多,数组很难直接表示出来,我们采用二进制状态压缩存储. 用$dp[i ...
- 题解 [BZOJ4710] 分特产
题面 解析 step 1 我们先考虑下有人没有的情况吧, 那对于每个特产就是放隔板的情况了, 设\(a[i]\)为第\(i\)个特产的个数, 那么第\(i\)个特产的方案数就是\(C_{a[i]+n- ...
- element ui的表格列设置fixed后做动态表格出现表格错乱
最近使用element-UI时,使用table做动态表格,当操作列使用fixed时,动态切换表格列设置设置时就会出现错乱,情况如下: 解决方法: 把el-table-column上的key设成一个随机 ...
- map(callback)将一组元素转换成其他数组(不论是否是元素数组)
map(callback) 概述 将一组元素转换成其他数组(不论是否是元素数组) 你可以用这个函数来建立一个列表,不论是值.属性还是CSS样式,或者其他特别形式.这都可以用'$.map()'来方便的建 ...
- PHP mysqli_ping() 函数
定义和用法 mysqli_ping() 函数进行一个服务器连接,如果连接已断开则尝试重新连接. <?php // 假定数据库用户名:root,密码:123456,数据库:RUNOOB $con= ...