awk内建函数

字符串函数
sub(regular expression,substitution string);
sub(regular expression,substitution string,target string);
 [root@xieqichao ~]# awk '{sub("Tom","Tommy"); print}' employees   #这里使用Tommy替换了Tom。
  Tommy Jones       4424    5/12/66         543354

当正则表达式Tom在第一个域中第一次被匹配后,
他将被字符串"Tommy"替换,如果将sub函数的第三个参数改为$2,将不会有替换发生。

[root@xieqichao ~]# awk ‘{sub(“Tom”,“Tommy”,$1); print}’ employees
Tommy Jones 4424 5/12/66 543354

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
gsub(regular expression,substitution string);

gsub(regular expression,substitution string,target string);
和sub不同的是,如果第一个参数中正则表达式在记录中出现多次,那么gsub将完成多次替换,而sub只是替换第一次出现的。

index(string,substring)

该函数将返回第二个参数在第一个参数中出现的位置,偏移量从1开始。

   [root@xieqichao ~]# awk 'BEGIN{print index("hello","el")}'
    2

 
 
  • 1
  • 2
length(string)

该函数返回字符串的长度。

 [root@xieqichao ~]# awk 'BEGIN{print length("hello")}'
 5

 
 
  • 1
  • 2
substr(string,starting position)

substr(string,starting position,length of string)
该函数返回第一个参数的子字符串,其截取起始位置为第二个参数(偏移量为1),截取长度为第三个参数,如果没有该参数,则从第二个参数指定的位置起,直到string的末尾。

 [root@xieqichao ~]#  awk 'BEGIN{name = substr("Hello World",2,3); print name}'
 ell

 
 
  • 1
  • 2
match(string,regular expression)

该函数返回在字符串中正则表达式位置的索引,如果找不到指定的正则表达式就返回0.match函数设置内置变量RSTART为字符串中子字符串的开始位置,RLENGTH为到字字符串末尾的字符个数。

[root@xieqichao ~]# awk 'BEGIN{start=match("Good ole CHINA", /[A-Z]+$/); print start}'
   10

 
 
  • 1
  • 2

上例中的正则表达式[A-Z]+$表示在字符串的末尾搜索连续的大写字母。在字符串"Good ole CHINA"的第10个位置找到字符串"CHINA"。

[root@xieqichao ~]# awk 'BEGIN{start=match("Good ole CHINA", /[A-Z]+$/); print RSTART, RLENGTH}'
    10 5

 
 
  • 1
  • 2

RSTART表示匹配时的起始索引,RLENGTH表示匹配的长度。

[root@xieqichao ~]# awk 'BEGIN{string="Good ole CHINA";\
start=match(string, /[A-Z]+$/); print substr(string,RSTART, RLENGTH)}'
    CHINA

 
 
  • 1
  • 2
  • 3

这里将match、RSTART、RLENGTH和substr巧妙的结合起来了。

toupper(string)
tolower(string)

以上两个函数分别返回参数字符串的大写和小写的形式。

  [root@xieqichao ~]# awk 'BEGIN {print toupper("hello"); print tolower("WORLD")}'
  HELLO
  world

 
 
  • 1
  • 2
  • 3
split(string,array,field seperator)
split(string,array)

该函数使用作为第三个参数的域分隔符把字符串分隔为一个数组。如果第三个参数没有提供,则使用当前默认的FS值。

  [root@xieqichao ~]# awk 'BEGIN{split("11/20/2011",date,"/"); print date[2]}'
  20
  variable = sprintf("string with format specifiers ",expr1,expr2,...)

 
 
  • 1
  • 2
  • 3

该函数和printf的差别等同于C语言中printf和sprintf的差别。前者将格式化后的结果输出到输出流,而后者输出到函数的返回值中。

 [root@xieqichao ~]# awk 'BEGIN{line = sprintf("%-15s %6.2f ", "hello",4.2); print line}'
 hello             4.20

 
 
  • 1
  • 2
时间函数:
systime()

该函数返回当前时间距离1970年1月1日之间相差的秒数。

   [root@xieqichao ~]# awk 'BEGIN{print systime()}'
   1321369554

 
 
  • 1
  • 2
strftime()

时间格式化函数,其格式化规则等同于C语言中的strftime函数提供的规则,见以下列表:

数据格式含义
%aAbbreviated weekday name
%AFull weekday name
%bAbbreviated month name
%BFull month name
%cDate and time representation appropriate for locale
%dDay of month as decimal number (01 – 31)
%HHour in 24-hour format (00 – 23)
%IHour in 12-hour format (01 – 12)
%jDay of year as decimal number (001 – 366)
%mMonth as decimal number (01 – 12)
%MMinute as decimal number (00 – 59)
%pCurrent locale’s A.M./P.M. indicator for 12-hour clock
%SSecond as decimal number (00 – 59)
%UWeek of year as decimal number, with Sunday as first day of week (00 – 53)
%wWeekday as decimal number (0 – 6; Sunday is 0)
%WWeek of year as decimal number, with Monday as first day of week (00 – 53)
%xDate representation for current locale
%XTime representation for current locale
%yYear without century, as decimal number (00 – 99)
%YYear with century, as decimal number
    [root@xieqichao ~]# awk 'BEGIN{ print strftime("%D",systime())}'
    11/15/11
    [root@xieqichao ~]# awk 'BEGIN{ now = strftime("%T"); print now}'
    23:17:29

 
 
  • 1
  • 2
  • 3
  • 4
内置数学函数:
名称返回值
atan2(x,y)y,x范围内的余切
cos(x)余弦函数
exp(x)求幂
int(x)取整
log(x)自然对数
sin(x)正弦函数
sqrt(x)平方根

    [root@xieqichao ~]# awk 'BEGIN{print 31/3}'
    10.3333
    [root@xieqichao ~]# awk 'BEGIN{print int(31/3)}'
    10

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
自定义函数:

自定义函数可以放在awk脚本的任何可以放置模板和动作的地方。

 function name(parameter1,parameter2,...) {
     statements
     return expression
 }

 
 
  • 1
  • 2
  • 3
  • 4

给函数中本地变量传递值。只使用变量的拷贝。数组通过地址或者指针传递,所以可以在函数内部直接改变数组元素的值。函数内部使用的任何没有作为参数传递的变量都被看做是全局变量,也就是这些变量对于整个程序都是可见的。如果变量在函数中发生了变化,那么就是在整个程序中发生了改变。唯一向函数提供本地变量的办法就是把他们放在参数列表中,这些参数通常被放在列表的最后。如果函数调用没有提供正式的参数,那么参数就初始化为空。return语句通常就返回程序控制并向调用者返回一个值。

 [root@xieqichao ~]# cat grades
 20 10
 30 20
 40 30

[root@xieqichao ~]# cat add.sc
function add(first,second) {
return first + second
}
{ print add($1,$2) }

[root@xieqichao ~]# awk -f add.sc grades
30
50
70

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
                <img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">解启超</a></span>
                                        </div>
                <div class="text"><span>发布了354 篇原创文章</span> · <span>获赞 52</span> · <span>访问量 3万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm attented bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">已关注</a>
                                </div>
                        </div>
                </div>
</article>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹汇川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值