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

match函数的更多相关文章

  1. index+match函数在压实度中对盒号盒质量随机不重复的最佳使用

    首先按照升序排列好盒号和盒质量,使其一一对应, 盒号    盒重量    随机值rand()     随机值大小排列rank 1         2001       0.01             ...

  2. Excel——MATCH函数

    使用 MATCH 函数在范围单元格中搜索特定的项,然后返回该项在此区域中的相对位置. 1.参数说明: MATCH(lookup_value, lookup_array, [match_type]) l ...

  3. EXCEL 2010学习笔记 —— VLOOKUP函数 嵌套 MATCH 函数

    match index vlookup 等函数都是查找引用类函数,需要查找的时候关键变量只有两个,区域+位置,区域的选择注意是否需要锁定,位置的确定可以通过输入特定的行号和列号. match() ma ...

  4. js match函数注意

    match函数 String.prototype.match 参数 regexp 返回 返回包含所有匹配的数组,如果匹配失败返回Null. 数组第一项是整段字符串的匹配,第二项至以后都是捕获匹配. 注 ...

  5. 使用Index()+Match()函数实现更为灵活的VLookUp()

    上一篇 http://www.cnblogs.com/-SANG/p/8407017.html 文章中已经介绍了vlookup的用法. 今天要使用index+match实现更为灵活的vlookup 先 ...

  6. awk之match函数

    功能:match函数是用于个性化定制搜索模式. 例子: 文件内容: this is wang ,not wan that is chen, not che this is chen ,and wang ...

  7. Excel中MATCH函数的正确使用

    Excel中MATCH函数是一个很强大的辅助函数, MATCH函数语法为:MATCH(lookup_value,lookuparray,match-type) lookup_value:表示查询的指定 ...

  8. JS中exec函数与match函数的区别与联系

    总结: 正则规则的声明,两种方法: exec是RegExp类的匹配方法 match是字符串类的匹配方法 var reg = /aaa/g; var reg = new RegExp("aaa ...

  9. excel 两列值匹配取另外一列值 INDEX MATCH 函数

    =INDEX($D:$D,MATCH(K68,$C:$C,0))

随机推荐

  1. [BZOJ 2809] Dispatching

    Link:https://www.lydsy.com/JudgeOnline/problem.php?id=2809 Algorithm: 很容易看出此题贪心的思路: 只要在每个点的子树中贪心选取费用 ...

  2. 详细User-Agent大全

    目录: Android: Android 0.* Android 1.* Android 2.* Android 3.* Android 4.* Windows Phone OS BlackBerry ...

  3. Coherence装载数据的研究 - Invocation Service

    这里验证第三个方法,原理是将需要装载的数据分载在所有的存储节点上,不同的地方是利用了存储节点提供的InvocationService进行装载,而不是PreloadRequest, 原理如图 前提条件是 ...

  4. Flexbox兼容性语法汇总

    Flexbox版本 flexbox从第一次出现至今总共有三个语法版本,他们分别是: "display:box;"  —  2009年的老版本 "display:flexb ...

  5. Java练习:tips.Print

    在学习Java时和<编程导论(Java)>中,大量使用了重载的System.out.println()等类似的输出语句.特别是书籍中,一行语句中包括System.out.println会显 ...

  6. Can we say objects have attributes, states and behaviors?

    15down votefavorite 3 I was reading through Oracle's introduction to OOP concepts and I came across ...

  7. MS project 使用小技巧收集

    如何使用的url: http://www.cnblogs.com/wangfupeng1988/p/3648994.html  (好文,易上手) 一.  如何设置周末为工作日. 1. 在 “工具”-& ...

  8. centos 6.5 python2.6.6 zbar 安装

       经过数次折腾,终于搞明白了这个zbar的安装顺序.   1.先安装http://zbar.sourceforge.net/download.html 下的zbar,   2.python 安装z ...

  9. ssh只读事务的管理

    概念:从这一点设置的时间点开始(时间点a)到这个事务结束的过程中,其他事务所提交的数据,该事务将看不见!(查询中不会出现别人在时间点a之后提交的数据) 应用场合: 如果你一次执行单条查询语句,则没有必 ...

  10. 关于 -webkit-line-clamp 详解

    最近需要做个商品列表,在手机屏幕不太一样的市场里,如何做到列表中刚刚好显示2行,偶然间发现淘宝的手机版有用到-webkit-line-clamp来实现这种效果 限制在一个块元素显示的文本的行数. -w ...