免费注册 查看新帖 |

Chinaunix

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

AWK是ERE的吗 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-11-14 14:48 |只看该作者 |倒序浏览
awk应该是使用拓展正则表达式的吧,那为什么我下面的那个匹配诸如1988-12-15这样的年份的正则表达式不匹配呢?
代码如下
  1. #! /bin/sh

  2. awk '
  3. BEGIN {
  4.         mouthstr = "jan,feb,mar,apr,may"
  5.         mouthstr = mouthstr ",june,nuly,aug,sept,oct,nov,dec"
  6.        
  7.         split(mouthstr,mouth,",")
  8.         printf "input->"
  9. }
  10. $1 ~ /^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/{
  11.         split($1,date,"-")
  12.         print date[1]," ",mouth[date[2]]," ",date[3]
  13.         printf "input->"
  14.         next
  15. }
  16. END{
  17.         print "END"
  18. }'
复制代码

论坛徽章:
0
2 [报告]
发表于 2010-11-14 15:08 |只看该作者
回复 1# rpbear

  1. [oracle@s12071 ~]$ echo "1988-12-15" | awk --posix '/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/'
  2. 1988-12-15
  3. [oracle@s12071 ~]$ echo "1988-12-15" | awk '/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/'
  4. [oracle@s12071 `]$
复制代码

论坛徽章:
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 [报告]
发表于 2010-11-14 15:14 |只看该作者
  1. echo "1988-12-15" | awk --re-interval '/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/'
复制代码

论坛徽章:
0
4 [报告]
发表于 2010-11-14 15:20 |只看该作者
上面两个选项确实都可以
--re-interval Enable the use of interval expressions in regular expression matching (see Regular Expressions, below). Interval expressions were not traditionally available in the AWK language. The POSIX standard added them, to make awk and egrep consistent with each other. However, their use is likely to break old AWK programs, so gawk only provides them if they are requested with this option, or when --posix is specified.回复 3# Shell_HAT

论坛徽章:
0
5 [报告]
发表于 2010-11-14 15:21 |只看该作者
awk  默认不支持posix 标准的    也是默认情况下 {}这样的posix标准的正则表达式是无效的

可以加一个参数实现

     -W re-interval
       --re-interval
              Enable the use of interval expressions in regular expression matching (see Regular Expressions, below).  Interval expressions were not tradition-ally  available  in  the  AWK  language.  The POSIX standard added them, to make awk and egrep consistent with each other.  However, their use is likely to break old AWK programs, so gawk only provides them if they are requested with this option, or when --posix is specified.

用--posix参数 效果类似

论坛徽章:
0
6 [报告]
发表于 2010-11-15 02:25 |只看该作者
不错

论坛徽章:
0
7 [报告]
发表于 2010-11-15 09:47 |只看该作者
不懂问男人

论坛徽章:
0
8 [报告]
发表于 2010-11-15 13:15 |只看该作者
我一般都是这样的
alias awk='awk -W posix'
还有就是grep
扩展的是egrep或者是grep -E
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP