标签:bash 三元运算 格式 down 三元 match second 正则表达 形式
awk的内置函数有算术、字符串、时间、位操作和其它杂项的函数。
[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
参数:
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 其它函数
标签:bash 三元运算 格式 down 三元 match second 正则表达 形式
原文地址:https://www.cnblogs.com/zoe233/p/11949063.html