match函数

match(s, r [, a])

Return the position in s where the regular expression r occurs, or 0 if r is not present, and set 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 corre‐sponding 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. Return the position in s where the regular expression r occurs, or 0 if r is not present

  如果匹配到,match函数返回第一个字符的position;如果没有匹配到,match函数返回0。

2. set the values of RSTART and RLENGTH

  RSTART:若匹配到,则该值为 match函数 所匹配到的字符串的 开始位置;否则 该值为0。
  RLENGHT:若匹配到,则该值为 match函数 所匹配到的字符串的 总长度;否则 该值为-1。

 

tmp.txt的内容
frank@ubuntu:~/test/tmp$ cat tmp.txt

 

1. RSTART 和 RLENGTH的含义

frank@ubuntu:~/test/tmp$ awk '{if(match($0,/^[.+]$/)){print $0, RSTART, RLENGTH}}' tmp.txt
[Frank] 1 7
[Mike] 1 6

这句命令的作用是:匹配以"["开头和以"]" 结尾的内容。
RSTART:若匹配到,则该值为 match函数 所匹配到的字符串的 开始位置;否则 该值为0。
RLENGHT:若匹配到,则该值为 match函数 所匹配到的字符串的 总长度;否则 该值为-1。

2. match函数的返回值

frank@ubuntu:~/test/tmp$ awk -vr=0 '{r=match($0,/^[.+]$/); print $0, RSTART, RLENGTH, r}' tmp.txt
[Frank] 1 7 1
id=123 0 -1 0
age=18 0 -1 0
0 -1 0
[Mike] 1 6 1
id=456 0 -1 0
age=18 0 -1 0

Return the position in s where the regular expression r occurs, or 0 if r is not present
如果匹配到,match函数返回第一个字符的position;如果没有匹配到,match函数返回0

   3. ~ operator

正则匹配运算符
frank@ubuntu:~/test/tmp$ awk -vr=0 '{if($0~/^[.+]$/){print $0}}' tmp.txt
[Frank]
[Mike]


4. match 有第三个参数的情况

frank@ubuntu:~/test/tmp$ awk -vr=0 '{r=match($0,/^[.+]$/,a);{print $0, RSTART, RLENGTH, r, a[0]}}' tmp.txt
[Frank] 1 7 1 [Frank]
id=123 0 -1 0
age=18 0 -1 0
0 -1 0
[Mike] 1 6 1 [Mike]
id=456 0 -1 0
age=18 0 -1 0

原文地址:https://www.cnblogs.com/black-mamba/p/8875747.html