免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3775 | 回复: 17
打印 上一主题 下一主题

关于通配符与正则表达式的疑问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-20 16:20 |只看该作者 |倒序浏览
在redhat enterprise 5 上安装oracle10g 在准备工作中要先安装几个包,
我将系统盘挂载上之后,mount /dev/cdrom  /mnt/cdrom
                              cd /mnt/cdrom/Server
                               ls |grep libXp*  此时不会显示任何内容
             而输入         ls |grep libXp.*之后则显示出该系统盘上有libXp开头的四个包
这又不是正则表达式吧?这该是通配符的用法吧?为何这样呢?

论坛徽章:
0
2 [报告]
发表于 2012-03-20 16:23 |只看该作者
当我输入 rpm -ivh /mnt/cdrom/Server/libXp* 时只会安装libXpm的两个包 而libXp-的两个包不能安装
当我输入  rpm -ivh /mnt/cdrom/Server/libXp-*才会安装libXp-的两个包

论坛徽章:
0
3 [报告]
发表于 2012-03-20 17:43 |只看该作者
按我的理解
ls abc*的*才是通配符

grep后面跟的应该按正则解析了,请高手。。。。

论坛徽章:
0
4 [报告]
发表于 2012-03-20 18:11 |只看该作者
回复 1# vnb2018


    情况应该是这样的:

  1. @2002686 ~
  2. $ ls
  3. lsof_4.85          lsof_4.855555   net-tools-1.60.tar.bz2
  4. lsof_4.85.tar.bz2  net-tools-1.60


  5. $ ls | grep lsof_4.8555*
  6. lsof_4.855555


  7. $ ls | grep lsof_4.*


  8. $ ls | grep lsof_4.?*


  9. $ ls | grep lsof_4.85?


  10. $ ls | grep lsof_4.85?*


  11. $ ls | grep lsof_4.85.*
  12. lsof_4.85.tar.bz2


  13. $ ls | grep lsof_4.8.*
  14. lsof_4.85
  15. lsof_4.85.tar.bz2
  16. lsof_4.855555


  17. $
复制代码
grep 表达式中*是匹配前面的单个字符0或者多次
man grep      
       A regular expression may be followed by one of several repetition oper-
       ators:
       ?      The preceding item is optional and matched at most once.
       *      The preceding item will be matched zero or more times.
       +      The preceding item will be matched one or more times.
       {n}    The preceding item is matched exactly n times.
       {n,}   The preceding item is matched n or more times.
       {n,m}  The  preceding  item  is  matched at least n times, but not more
              than m times.

论坛徽章:
0
5 [报告]
发表于 2012-03-20 20:51 |只看该作者
本帖最后由 yuhuohu 于 2012-03-20 20:53 编辑

回复 4# 南极雨


    你说的我也明白,但我没法解释这个:
  1. # ls /tmp/a*
  2. /tmp/a  /tmp/a b  /tmp/a c  /tmp/ablockfile
  3. # ls /tmp/|grep a*
  4. # ls /tmp/|grep a?
  5. # ls /tmp/|grep a+
  6. #
复制代码
用grep -E的话,除了a*不行,a?和a+都是可以有东西输出

论坛徽章:
0
6 [报告]
发表于 2012-03-20 21:02 |只看该作者
本帖最后由 loveacat 于 2012-03-20 21:08 编辑
yuhuohu 发表于 2012-03-20 20:51
回复 4# 南极雨

grep之后是用的正则表达式,而ls用的是通配符
因为?在正则表达式里面匹配0或1个前一字符,而+表示1个以上的前一字符
所以grep a?或者grep a+会有输出,a*表示以0个以上连续的a,结果就没输出了。

论坛徽章:
0
7 [报告]
发表于 2012-03-20 21:10 |只看该作者
回复 6# loveacat


    我的grep
a*
a+
a?
都没输出
除非用grep -E

论坛徽章:
0
8 [报告]
发表于 2012-03-20 21:18 |只看该作者
yuhuohu 发表于 2012-03-20 21:10
回复 6# loveacat

?和+都是扩展的正则表达式,所以要用-E选项,而普通的grep匹配不了

论坛徽章:
0
9 [报告]
发表于 2012-03-20 21:22 |只看该作者
回复 8# loveacat


    THKS,shell这些细节的东西还真是很多没注意到

论坛徽章:
0
10 [报告]
发表于 2012-03-21 11:38 |只看该作者
好蛋疼
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP