1. 内置函数

awk的内置函数有算术、字符串、时间、位操作和其它杂项的函数。

 

1.1 算术函数

  • atan2(y,x)  返回弧度的反正切(y/x)
  • cos(expr)  返回expr的余弦(以弧度形式)
  • exp(expr)  此函数用作找到指数值。
  • int(expr)  取整
  • log(expr)  计算自然对数
  • rand()   返回随机数,0和1之间,[0,1)
  • sin(expr)  正弦(以弧度形式)
  • sqrt(expr)  平方根
  • srand([expr])  产生种子值的随机数。使用expr作为随机数生成的新的种子。如果没有expr,使用一天的时间值作为种子值。
[root@oldboy test]# awk 'BEGIN{print int(13.2)}'     # 取整 
13
[root@oldboy test]# awk 'BEGIN{print log(13.2)}'   # 自然对数
2.58022
[root@oldboy test]# awk 'BEGIN{print rand()}'       # 随机数
0.237788
[root@oldboy test]# awk 'BEGIN{print srand()}' # 随机数种子 1 [root@oldboy test]# awk 'BEGIN{print srand(111)}' 1 [root@oldboy test]# awk 'BEGIN{print srand(13.2)}' 1 [root@oldboy test]# awk 'BEGIN{print rand()}' # 固定不变的随机数 0.237788 [root@oldboy test]# awk 'BEGIN{print rand()}' 0.237788 [root@oldboy test]# awk 'BEGIN{print rand()}' 0.237788

[root@oldboy test]# awk 'BEGIN{print sqrt(13.2)}' # 平方根 3.63318

 

1.2 字符串函数

  • gsub(Ere, Repl, [ln])  和sub函数完全一样使用。区别:gsub全部替换,sub只替换匹配正则的第一个具体值。
  • sub(Ere, Repl, [ln])   
    • 用Repl参数指定的字符串题替换 ln 参数指定的字符串中的由 Ere参数指定的扩展正则表达式的第一个具体值。
    • sub函数返回替换的数量。
    • 出现在Repl参数指定的字符串中的&(and符号)由ln参数指定的与Ere参数的指定的扩展正则表达式匹配的字符串替换。
    • 如果未指定ln参数,缺省值是整个记录($0)
  • index(str1, str2)
    • 在str1中如果有出现str2,则返回在str1中的位置;若str2不存在,则返回0.
    • 字符串的索引起始值为1。
  • length[(str)]
    • 返回str的字符串长度(字符形式)
    • 如未给出str参数,则返回整个记录的长度($0)
  • substr(str, start, [length])
    • 从字符串str中,起始位置start取长度为length的子字符串。
    • 如为指定参数length,则子串的长度将是从start位置到str的结尾的长度。
  • match(str, Ere)
    • 匹配正则的模式,如str中存在,则返回匹配的位置
    • 如不存在,则返回0
  • split(str, Arr, [Ere])
    • 将str通过Ere分割成array
  • tolower(str) 小写化
  • toupper(str) 大写化
  • sprintf(Format, Expr, Expr,...)
    • 根据format参数指定的prinf子例格式字符串来格式化Expr参数指定的表达式,并返回最后生成的字符串。

参数:

    • str字符串;
    • Ere扩展正则表达式
    • Repli 替换字符串

示例:

gsub替换:在info中查找满足正则表达式,/[0-9]+/用"!" 替换,并且替换后的值,赋值给info未给info值,默认是$0。

[root@oldboy test]# awk 'BEGIN{info="this is a test 2010test!";gsub(/[0-9]+/,"!",info); print info}'       
this is a test !test!

 

sub替换:替换匹配正则的第一个值;gsub替换所有。

[root@oldboy test]# awk '{sub(/\//,"?");print $0}' awk_test_file.txt  
root:x:0:0:root:?root:/bin/bash
bin:x:1:1:bin:?bin:/sbin/nologin
daemon:x:2:2:daemon:?sbin:/sbin/nologin
adm:x:3:4:adm:?var/adm:/sbin/nologin
lp:x:4:7:lp:?var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:?sbin:/bin/sync
shutdown:x:6:0:shutdown:?sbin:/sbin/shutdown
halt:x:7:0:halt:?sbin:/sbin/halt
mail:x:8:12:mail:?var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:?var/spool/uucp:/sbin/nologin
[root
@oldboy test]# awk '{gsub(/\//,"?");print $0}' awk_test_file.txt root:x:0:0:root:?root:?bin?bash bin:x:1:1:bin:?bin:?sbin?nologin daemon:x:2:2:daemon:?sbin:?sbin?nologin adm:x:3:4:adm:?var?adm:?sbin?nologin lp:x:4:7:lp:?var?spool?lpd:?sbin?nologin sync:x:5:0:sync:?sbin:?bin?sync shutdown:x:6:0:shutdown:?sbin:?sbin?shutdown halt:x:7:0:halt:?sbin:?sbin?halt mail:x:8:12:mail:?var?spool?mail:?sbin?nologin uucp:x:10:14:uucp:?var?spool?uucp:?sbin?nologin

 

 

index索引值:

[root@oldboy test]# awk '{print index($0,"nologin")}' awk_test_file.txt     
0
26
33
30
34
0
0
0
40
41

 

 

length取字符串长度,substr取子字符串:

[root@oldboy test]# awk 'BEGIN{str="this is a line.\n this is second line.";print length(str)}'       
37

[root@oldboy test]# awk 'BEGIN{str="this is a line.\n this is second line.";print substr(str,20,5)}'
is is
[root@oldboy test]# awk 'BEGIN{str="this is a line.\n this is second line.";print substr(str,20)}'  
is is second line.

  

match匹配查找:结合三元运算显示是否成功匹配

[root@oldboy test]# awk 'BEGIN{info="this is a test2010test!"; print match(info,/[0-9]+/) ? "ok": "no found";}'
ok

 

 

split分割:

[root@oldboy test]# awk 'BEGIN{info="this is a test";split(info,tA," ");for (k in tA){print k,tA[k]}}'       4 test
1 this
2 is
3 a

 

 

1.3 时间函数

 

1.4 位操作函数

 

1.5 其它函数

 

 

2. 自定义函数

 

posted on 2019-11-28 12:03  Zoe233  阅读(355)  评论(0编辑  收藏  举报