免费注册 查看新帖 |

Chinaunix

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

shell下正则表达式写法咨询 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-02-15 17:36 |只看该作者 |正序浏览
请教各位,在shell下用正则表达式怎样写能筛选出下面这样的文件列表:

epcisqhpremdayhis10.mdp
epcisqhpremdayhis11.mdp
epcisqhpremdayhis12.mdp
epcisqhpremdayhis13.mdp
epcisqhpremdayhis14.mdp
epcisqhpremdayhis15.mdp
epcisqhpremdayhis16.mdp
epcisqhpremdayhis17.mdp
epcisqhpremdayhis18.mdp
epcisqhpremdayhis19.mdp
epcisqhpremdayhis1.mdp
epcisqhpremdayhis20.mdp
epcisqhpremdayhis21.mdp
epcisqhpremdayhis22.mdp
epcisqhpremdayhis23.mdp
epcisqhpremdayhis24.mdp
epcisqhpremdayhis25.mdp
epcisqhpremdayhis26.mdp
epcisqhpremdayhis27.mdp
epcisqhpremdayhis28.mdp
epcisqhpremdayhis2.mdp
epcisqhpremdayhis3.mdp
epcisqhpremdayhis4.mdp
epcisqhpremdayhis5.mdp
epcisqhpremdayhis6.mdp
epcisqhpremdayhis7.mdp
epcisqhpremdayhis8.mdp
epcisqhpremdayhis9.mdp
epcisqhpremdayhis.mdc

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
14 [报告]
发表于 2012-02-16 10:53 |只看该作者
回复 13# dulingqiang


    是不是选项没有打开呢?
bash:
  1. $ ls file*.txt
  2. file.txt  file1.txt  file22.txt  filefoo.txt

  3. $ shopt extglob
  4. extglob         on

  5. $ ls file?([0-9])?([0-9]).txt
  6. file.txt  file1.txt  file22.txt
复制代码

论坛徽章:
0
13 [报告]
发表于 2012-02-16 10:38 |只看该作者
大家提供的方法好几种验证在bash下支持是报错的

abc163635 这个基本符合需求,不过还有个小问题,有些文件名中不含数字的,还是没查出来

abc163635 发表于 2012-02-15 19:03

论坛徽章:
0
12 [报告]
发表于 2012-02-16 08:53 |只看该作者
  1. grep 'epcisqhpremdayhis[0-9]\{0,2\}\.md[c|p]' file
复制代码

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
11 [报告]
发表于 2012-02-15 22:34 |只看该作者
回复 1# dulingqiang


    如果是 bash 的 path name expansion, extglob on:
  1. ls epcisqhpremdayhis?([0-9])?([0-9]).md[cp]
复制代码

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
10 [报告]
发表于 2012-02-15 22:30 |只看该作者
回复 9# zooyo


    分开写。

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
9 [报告]
发表于 2012-02-15 20:15 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
8 [报告]
发表于 2012-02-15 20:10 |只看该作者
本帖最后由 cooluncle 于 2012-02-16 09:07 编辑

find <path>  -regex ".*/epcisqhpremdayhis[0-9]\.\(mdp\|mdc\)$" -o -regex ".*/epcisqhpremdayhis[0-9][0-9]\.\(mdp\|mdc\)$"   -type f

论坛徽章:
0
7 [报告]
发表于 2012-02-15 19:29 |只看该作者
ls | sed -n '/[a-z]\+[0-9]\+\.md[cp]/p'

论坛徽章:
0
6 [报告]
发表于 2012-02-15 19:03 |只看该作者
  1. ls | grep "epcisqhpremdayhis[0-9]\{1,2\}.md[cp]"
复制代码
  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP