免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2178 | 回复: 7

awk包含匹配正则表达式问题 [复制链接]

论坛徽章:
0
发表于 2012-04-19 16:13 |显示全部楼层
本帖最后由 mgqw 于 2012-04-19 16:15 编辑

为什么我直接匹配两个'+'号 /^\+\+/ 能匹配上,
而用正则表达式 /^\+{2,}/ 匹配两次以上就匹配不上了,
为什么呢? 求解释
  1. $ cat svn.log
  2. +++ xxxconfig.js
  3. +               'xxxSettingInfo'         
  4. +               'xxxChangeSet'           
  5. +++ yyyconfig.js
  6. +               'yyySettingInfo'
  7. +               'yyyChangeSet'

  8. $
  9. $
  10. $ awk '{ if($1~/^\+\+/){print $0} }' ./svn.log  
  11. +++ xxxconfig.js
  12. +++ yyyconfig.js
  13. $
  14. $
  15. $
  16. $ awk '{ if($1~/^\+{2,}/){print $0} }' ./svn.log
  17. $
  18. $
  19. $
复制代码

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
发表于 2012-04-19 16:17 |显示全部楼层
Interval expressions were not traditionally available in awk. They were added as part of the POSIX standard to make awk and egrep consistent with each other.

Initially, because old programs may use ‘{’ and ‘}’ in regexp constants, gawk did not match interval expressions in regexps.

论坛徽章:
0
发表于 2012-04-19 16:28 |显示全部楼层
回复 2# waker


    版主威武, 这么快就回答了!~

论坛徽章:
0
发表于 2012-04-19 16:30 |显示全部楼层
# echo "+++ abc"|awk --posix '/^\+{2,}/'
+++ abc

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
发表于 2012-04-19 16:32 |显示全部楼层
说明你发了个经帖

论坛徽章:
0
发表于 2012-04-19 16:36 |显示全部楼层
我有罪......

论坛徽章:
0
发表于 2012-04-19 16:40 |显示全部楼层
       r{n,m}     One or two numbers inside braces denote an interval expression.  If there is one number in the braces, the preceding regular  expression
                  r is repeated n times.  If there are two numbers separated by a comma, r is repeated n to m times.  If there is one number followed by a
                  comma, then r is repeated at least n times.
                  Interval expressions are only available if either --posix or --re-interval is specified on the command line.

论坛徽章:
0
发表于 2012-04-19 17:44 |显示全部楼层
用--re-interval 参数可以
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP