免费注册 查看新帖 |

Chinaunix

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

awk正则表达式匹配 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-04-11 16:39 |只看该作者 |倒序浏览
在awk中能不能用正则表达式匹配重复指定次数

比如,我要匹配有连续三个字符出现的字符串
我试了  [a-zA-z]{3} 是不行的啊

还请高手指点

论坛徽章:
0
2 [报告]
发表于 2010-04-11 17:07 |只看该作者
{n}或\{n\} 仅定义 --posix 或 --re-interval有效(要表达}和{,得用\\{和\\} 没有定义--posix或--re-interval时,不能用{n}的语法

论坛徽章:
0
3 [报告]
发表于 2010-04-11 17:13 |只看该作者
回复 1# alexandnpu


    版本问题~我的也不支持~ 书上说在POSIX之前的版本都不支持·但什么事POSIX之前的版本我也没研究过

论坛徽章:
0
4 [报告]
发表于 2010-04-11 17:28 |只看该作者
{n}或\{n\} 仅定义 --posix 或 --re-interval有效(要表达}和{,得用\\{和\\} 没有定义--posix或--re-interva ...
bbgg1983 发表于 2010-04-11 17:07



还请讲解一下,没看明白

论坛徽章:
0
5 [报告]
发表于 2010-04-11 18:09 |只看该作者
本帖最后由 crowsy001 于 2010-04-11 22:06 编辑
还请讲解一下,没看明白
alexandnpu 发表于 2010-04-11 17:28


    发现不加,符号也可以。  man 也会误导, 实践。。
    man awk 看看, 我这里支持。

{m,}
     Specifies that a string matches if at least m occurrences of the pattern are within the string. The command line:

     awk '/t{2,}/' testfile

     prints to standard output:

     smitters, alexis
{m,n}
     Specifies that a string matches if between m and n, inclusive, occurrences of the pattern are within the string (
     where m <= n). The command line:

     awk '/er{1, 2}/' testfile

     prints to standard output:

     smithern, harry
     smithern, anne
     smitters, alexis

论坛徽章:
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
6 [报告]
发表于 2010-04-11 18:20 |只看该作者

  1. awk --re-interval '/[a-zA-z]{3}/{print}' urfile
复制代码

论坛徽章:
0
7 [报告]
发表于 2010-04-11 18:59 |只看该作者
回复 6# Shell_HAT


    学习了~

论坛徽章:
20
CU大牛徽章
日期:2013-04-17 11:48:26羊年新春福章
日期:2015-03-10 22:39:202015年中国系统架构师大会
日期:2015-06-29 16:11:282015亚冠之平阳省
日期:2015-07-31 09:19:042015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-09-30 06:20:002015亚冠之柏太阳神
日期:2015-10-19 20:29:5915-16赛季CBA联赛之天津
日期:2016-11-29 14:03:4315-16赛季CBA联赛之北控
日期:2016-12-24 20:51:492015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-12 20:58:532014年中国系统架构师大会
日期:2014-10-14 15:59:00
8 [报告]
发表于 2010-04-11 19:37 |只看该作者
Shell_HAT 发表于 2010-04-11 18:20



    re-inerval在这儿是什么意思,兄弟可否解释一下

论坛徽章:
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
9 [报告]
发表于 2010-04-11 19:56 |只看该作者
回复 8# beyondfly


在ABS里面搜索--re-interval:Advanced Bash-Scripting Guide 3.9.1 中文版.pdf
http://bbs.chinaunix.net/thread-1610033-1-1.html

论坛徽章:
0
10 [报告]
发表于 2010-04-11 22:15 |只看该作者
回复  beyondfly


在ABS里面搜索--re-interval:Advanced Bash-Scripting Guide 3.9.1 中文版.pdf
Shell_HAT 发表于 2010-04-11 19:56


使用大括号形式的RE是不能够在"经典"(非POSIX兼容)的awk版本中正常运行的. 然而, gawk命令中有一个--re-interval选项, 使用这个选项就允许使
用大括号形式的RE了(无需转义).

好像是gawk 我的awk报错的。
sy@/home/t/a/crowsy/sh>awk --re-interval '/[a-z]{3,}/{print}' txt
awk: Not a recognized flag: -
Usage: awk [-F Character][-v Variable=Value][-f File|Commands][Variable=Value|File ...]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP