awk的match函数总结

开始正文之前, 推荐下这里有个介绍awk数组的精华帖:  http://bbs.chinaunix.net/thread-2312439-1-1.html
 
grep 1083628889 XXYY.TamServer_updateVipAmount_20121227.log |  tr -d '][' | awk 'BEGIN{ FS="|" }{ match($10, / money=[0-9]+*/, a);  match($10, / vip=[0-9]+*/, b);  print $4,a[0],b[0] }'
 
match(s, r [, a])
 Returns  the  position  in  s  where the regular expression r occurs, or 0 if r is not present, and sets the values of RSTART and RLENGTH.  Note that the argument order is the same as for the ~ operator: str ~ re.  If  array  a  is  provided,  a  is  cleared  and  then  elements 1 through n are filled with the portions of s that match the corresponding parenthesized subexpression in r.  The 0'th element of a contains the portion of  s  matched  by  the  entire  regular expression  r.   Subscripts  a[n,  "start"],  and  a[n,  "length"] provide the starting index in the string and length    respectively, of each matching substring.
 
两种用法:
 1. 普通用法
 match(字符串,正则表达式)
 内置变量RSTART表示匹配开始的位置,RLENGTH表示匹配的长度
 如果匹配到了,返回匹配到的开始位置,否则返回0
 $ awk 'BEGIN{start=match("Abc Ef Kig",/ [A-Z][a-z]+ /);print RSTART,RLENGTH}'
 4 4
 
 2. 建立数组(If  array  a  is  provided,  a  is  cleared  and  then  elements 1 through n are filled with the portions of s that match the corresponding
 parenthesized subexpression in r.  The 0'th element of a contains the portion of  s  matched  by  the  entire  regular
 expression  r.   Subscripts  a[n,  "start"],  and  a[n,  "length"] provide the starting index in the string and length
    respectively, of each matching substring.)
 
 echo "foooobazbarrrrr | gawk '{ match($0, /(fo+).+(bar*)/, arr)  #匹配到的部分自动赋值到arr中,下标从1开始
          print arr[1], arr[2]
          print arr[1, "start"], arr[1, "length"]  #二维数组arr[index,"start"]值=RSTART
          print arr[2, "start"], arr[2, "length"]  #二维数组arr[index,"length"]值=RLENGTH
    }'
 
 foooo barrrrr
 1 5
 9 7
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值