开始正文之前,推荐下这里有个介绍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分钟