免费注册 查看新帖 |

Chinaunix

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

awk匹配正则问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-05 21:15 |只看该作者 |倒序浏览
例子:
               9360342 Query   SET character_set_connection='utf8',character_set_results='utf8',character_set_client=binary, sql_mo
de=''
                9360342 Query   SET character_set_connection='utf8',character_set_results='utf8',character_set_client=binary, sql_mo
de=''
                9360340 Quit
                9360338 Quit

我用awk匹配行中带有7个数字的,怎么也匹配不上。
awk '{if($1~/\[0-9\]\{7\}/){print $1} else print "err"}'
用$0全行匹配也不行
awk '{if($0~/\[0-9\]\{7\}/){print $1} else print "err"}'

论坛徽章:
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
2 [报告]
发表于 2012-03-05 21:20 |只看该作者
回复 1# nugget

interval expression:
    http://bbs.chinaunix.net/forum.p ... ;page=1#pid12638391

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
3 [报告]
发表于 2012-03-05 21:29 |只看该作者
  1. awk --re-interval '{if($1~/[0-9]{7}/){print $1} else print "err"}' urfile
复制代码

论坛徽章:
13
双鱼座
日期:2013-10-23 09:30:05数据库技术版块每日发帖之星
日期:2016-04-20 06:20:00程序设计版块每日发帖之星
日期:2016-03-09 06:20:002015亚冠之塔什干火车头
日期:2015-11-02 10:07:452015亚冠之德黑兰石油
日期:2015-08-30 10:07:07数据库技术版块每日发帖之星
日期:2015-08-28 06:20:00数据库技术版块每日发帖之星
日期:2015-08-05 06:20:002015年迎新春徽章
日期:2015-03-04 09:57:09辰龙
日期:2014-12-03 14:45:52酉鸡
日期:2014-07-23 09:46:23亥猪
日期:2014-03-13 08:46:22金牛座
日期:2014-02-11 09:36:21
4 [报告]
发表于 2012-03-06 08:59 |只看该作者
--re-interval支持区间匹配

论坛徽章:
0
5 [报告]
发表于 2013-03-01 15:34 |只看该作者
nugget 发表于 2012-03-05 21:15
例子:
               9360342 Query   SET character_set_connection='utf8',character_set_results='u ...




最佳解释:

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.

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
6 [报告]
发表于 2013-03-01 16:21 |只看该作者
本帖最后由 jason680 于 2013-03-01 16:25 编辑

sorry, I got a mistake

论坛徽章:
0
7 [报告]
发表于 2013-03-01 16:39 |只看该作者
本帖最后由 todayhero 于 2013-03-01 16:40 编辑
  1. [root@GO-EMAIL-1 ~]# awk --posix '$0 ~ /[0-9]{7}/' urfile   
  2. 9360342 Query   SET character_set_connection='utf8',character_set_results='utf8',character_set_client=binary, sql_mo
  3. 9360342 Query   SET character_set_connection='utf8',character_set_results='utf8',character_set_client=binary, sql_mo
  4. 9360340 Quit
  5. 9360338 Quit
复制代码
加--posix也没问题吧!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP