当正则表达式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函数提供的规则,见以下列表:
数据格式 | 含义 |
---|---|
%a | Abbreviated weekday name |
%A | Full weekday name |
%b | Abbreviated month name |
%B | Full month name |
%c | Date and time representation appropriate for locale |
%d | Day of month as decimal number (01 – 31) |
%H | Hour in 24-hour format (00 – 23) |
%I | Hour in 12-hour format (01 – 12) |
%j | Day of year as decimal number (001 – 366) |
%m | Month as decimal number (01 – 12) |
%M | Minute as decimal number (00 – 59) |
%p | Current locale’s A.M./P.M. indicator for 12-hour clock |
%S | Second as decimal number (00 – 59) |
%U | Week of year as decimal number, with Sunday as first day of week (00 – 53) |
%w | Weekday as decimal number (0 – 6; Sunday is 0) |
%W | Week of year as decimal number, with Monday as first day of week (00 – 53) |
%x | Date representation for current locale |
%X | Time representation for current locale |
%y | Year without century, as decimal number (00 – 99) |
%Y | Year 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="{"mod":"popu_824"}"><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="{"mod":"1582594662_002"}"><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="{"mod":"popu_379"}" 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="{"mod":"popu_379"}">已关注</a>
</div>
</div>
</div>
</article>