awk的内置函数大致可以分类为算数函数、字符串函数、时间函数、其他函数等
算数函数
最常用的算数函数有rand函数、srand函数、int函数。
可以使用rand函数生成随机数,但是使用rand函数时,需要配合srand函数,否则rand函数返回的值将一直不变
ghostwu@dev:~/linux/awk$ awk 'BEGIN { print rand(); }'0.237788ghostwu@dev:~/linux/awk$ awk 'BEGIN { print rand(); }'0.237788ghostwu@dev:~/linux/awk$ awk 'BEGIN { srand(); print rand(); }'0.543596ghostwu@dev:~/linux/awk$ awk 'BEGIN { srand(); print rand(); }'0.524347ghostwu@dev:~/linux/awk$ awk 'BEGIN { srand(); print rand(); }'0.235681
用int函数取整
ghostwu@dev:~/linux/awk$ awk 'BEGIN { srand(); print 10 * rand(); }'7.83585ghostwu@dev:~/linux/awk$ awk 'BEGIN { srand(); print 10 * rand(); }'8.53695ghostwu@dev:~/linux/awk$ awk 'BEGIN { srand(); print int( 10 * rand() ); }'6ghostwu@dev:~/linux/awk$ awk 'BEGIN { srand(); print int( 10 * rand() ); }'8ghostwu@dev:~/linux/awk$ awk 'BEGIN { srand(); print int( 10 * rand() ); }'9
字符串函数
gsub函数或sub函数替换指定文本字符串
1,不指定第三个参数,默认替换整行中匹配的字符串
ghostwu@dev:~/linux/awk$ awk '{ gsub( "n", "N" ); print }' ghostwu.txt ghostwu 20 maNzhaNgsaN 22lisighostwu 30 maNzhaNzhao 40 maNpeter 20 maNzhaNzhao 30 maNghostwu@dev:~/linux/awk$ cat ghostwu.txt ghostwu 20 manzhangsan 22lisighostwu 30 manzhanzhao 40 manpeter 20 manzhanzhao 30 man
2,可以指定第三个参数,替换指定的范围,如第一列
ghostwu@dev:~/linux/awk$ awk '{ gsub( "n", "N", $1 ); print }' ghostwu.txt ghostwu 20 manzhaNgsaN 22lisighostwu 30 manzhaNzhao 40 manpeter 20 manzhaNzhao 30 man
还可以支持正则表达式
ghostwu@dev:~/linux/awk$ awk '{ gsub( "[a-z]", "G", $1 ); print }' ghostwu.txt GGGGGGG 20 manGGGGGGGG 22GGGGGGGGGGG 30 manGGGGGGGG 40 manGGGGG 20 manGGGGGGGG 30 man
sub替换的是第一次出现的字符串
ghostwu@dev:~/linux/awk$ awk '{ sub( "n", "N" ); print }' ghostwu.txt ghostwu 20 maNzhaNgsan 22lisighostwu 30 maNzhaNzhao 40 manpeter 20 maNzhaNzhao 30 man
length:获取每一行文本的长度
ghostwu@dev:~/linux/awk$ awk '{ print $0, length() }' ghostwu.txt ghostwu 20 man 14zhangsan 22 11lisi 4ghostwu 30 man 14zhanzhao 40 man 15peter 20 man 12zhanzhao 30 man 15
index: 获取字符串的位置,没有该字符串,就是0
ghostwu@dev:~/linux/awk$ awk '{print $0, index( $0, "n" )}' ghostwu.txt ghostwu 20 man 14zhangsan 22 4lisi 0ghostwu 30 man 14zhanzhao 40 man 4peter 20 man 12zhanzhao 30 man 4
split:字符串切割成数组,每行切割的结果存储在数组arr中,返回值为数组的长度
ghostwu@dev:~/linux/awk$ awk '{ len = split( $0, arr, " " ); for ( i in arr ) { print i, arr[i], len } }' ghostwu.txt 1 ghostwu 32 20 33 man 31 zhangsan 22 22 21 lisi 11 ghostwu 32 30 33 man 31 zhanzhao 32 40 33 man 31 peter 32 20 33 man 31 zhanzhao 32 30 33 man 3ghostwu@dev:~/linux/awk$ cat ghostwu.txt ghostwu 20 manzhangsan 22lisighostwu 30 manzhanzhao 40 manpeter 20 manzhanzhao 30 man
asort:把数组的值从小到大排序,但是索引也发生改变了
ghostwu@dev:~/linux/awk$ awk 'BEGIN{arr["a"] = 12; arr["b"]=5; arr["c"] = 1; arr["d"]=8; for( i in arr ) { print i, arr[i] } }'a 12b 5c 1d 8ghostwu@dev:~/linux/awk$ awk 'BEGIN{arr["a"] = 12; arr["b"]=5; arr["c"] = 1; arr["d"]=8; asort( arr ); for( i in arr ) { print i, arr[i] } }'1 12 53 84 12
可以是用第二个参数 用来保存原数组排序后的结果,同时原数组保持不变
ghostwu@dev:~/linux/awk$ awk 'BEGIN{arr["a"] = 12; arr["b"]=5; arr["c"] = 1; arr["d"]=8; asort( arr, newarr ); for( i in newarr ) { print i, newarr[i] } }'1 12 53 84 12ghostwu@dev:~/linux/awk$ awk 'BEGIN{arr["a"] = 12; arr["b"]=5; arr["c"] = 1; arr["d"]=8; asort( arr, newarr ); for( i in arr ) { print i, arr[i] } }'a 12b 5c 1d 8
asort的返回值为数组中的元素个数
ghostwu@dev:~/linux/awk$ awk 'BEGIN{arr["a"] = 12; arr["b"]=5; arr["c"] = 1; arr["d"]=8; len = asort( arr, newarr ); for( i in arr ) { print len, i, arr[i] } }' 4 a 124 b 54 c 14 d 8
当数组的下标为字符串时,asorti 函数会根据原数组中的下标的字母顺序进行排序