zoukankan      html  css  js  c++  java
  • gsub函数

    gsub(r, s [, t])
      
    For each substring matching the regular expression r in the string t, substitute the string s, and return the number of substitutions.  If t is not supplied,  use  $0. An  &  in  the replacement text is replaced with the text that was actually matched.  Use & to get a literal &.  (This must be typed as "\&"; see GAWK: Effective AWK Programming for a fuller discussion of the rules for &'s and backslashes in the replacement text of sub(), gsub(), and gensub().)
     
    1. r 表示正则表达式,用来进行 查找指定特征的字符串;
    2. s 表示 替换的内容;
    3. t 表示 输入的字符串,如果没有指定t,默认是$0.(因为awk是行处理的,所以$0表示每一行);
    4. 返回值:匹配并替换的次数。
     
    示例:
    gsub_test文件的内容为:
    frank@ubuntu:~/test/acapela$ cat gsub_text
    [Frank]
    id=123
    age=18
    [Mike]
    id=456
    age=18
     
     
    frank@ubuntu:~/test/acapela$ awk '{s+=gsub(/^[/,"");print $0,s}' gsub_text
    Frank] 1
    id=123 1
    age=18 1
    1
    Mike] 2
    id=456 2
    age=18 2
     
     
    统计 以“[”开头的行的个数。即获取gsub_text中的人数。
    s+=gsub(/^[/,"") 表示 匹配以“[”开头的行并替换为"",同时将匹配并替换的次数保存在s中。
    print $0,s 为了方便查看执行的结果,一是输出每一次行处理的输入,同时打印当前 替换的次数s。
    从输出的结果可知,
    gsub_text中第一行 匹配到并替换,所以 s = 1;第 2,3,4行中没有匹配到"[",所以返回值为0,s加1依旧是1;第5行 又匹配到并替换,所以返回值是1,s加1变成2。
  • 相关阅读:
    数据结构-链式结构-队列
    c数据结构-线性结构-队列
    栈的四则运算的运用
    C++/C—函数指针
    向显存写数据x8086
    结构体内存偏移量
    MFC-ComboBox控件的使用
    MFC的Edit text编辑框控件
    C/C++反汇编-各种表达式(加减乘)
    MFC- 学习控件-静态文本
  • 原文地址:https://www.cnblogs.com/black-mamba/p/8862384.html
Copyright © 2011-2022 走看看