前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >awk 函数-awk的match函数总结

awk 函数-awk的match函数总结

作者头像
囍楽云
发布2022-12-26 14:13:37
2.1K0
发布2022-12-26 14:13:37
举报
文章被收录于专栏:囍楽云博客

  开始正文之前,推荐下这里有个介绍awk数组的精华帖:

  grep XXYY..log | tr -d '][' | awk 'BEGIN{ FS="|" }{ [match]2; print $4,a[0],b[0] }'

  match(s, r [, a])

   the in s where the r occurs, or 0 if r is not , and sets the values and . Note that the order is the same as for the ~ : str ~ re. If array a is , a is and then 1 n are filled with the of s that match the esized in r. The 0'th of a the of s by the entire r. a[n, "start"], and a[n, "length"] the index in the string and length , of each .

  两种用法:

  1. 普通用法

  match(字符串,正则表达式)

  内置变量RSTART表示匹配开始的位置awk 函数,表示匹配的长度

  如果匹配到了,返回匹配到的开始位置,否则返回0

  $ awk 'BEGIN{start=match("Abc Ef Kig",/ A-Z+ /);print RSTART,}'

  4 4

  2. 建立数组(If array a is , a is and then 1 n are filled with the of s that match the

   in r. The 0'th of a the of s by the entire

   r. a[n, "start"], and a[n, "length"] the index in the string and length

  , of each .)

  echo " | gawk '{ match($0, /(fo+).+(bar*)/, arr) #匹配到的部分自动赋值到arr中awk 函数,下标从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"]值=

  }'

  foooo

  1 5

  9 7

本文共 112 个字数,平均阅读时长 ≈ 1分钟

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档