awk - 数据提取和报告工具 (3) 函数

  • 原创
  • Madman
  • /
  • /
  • 0
  • 9117 次阅读

awk - 数据提取和报告工具 (3) 函数-min.png

Synopsis: 列举了awk中常用的内置函数,以及如何创建自定义函数。内置函数大致可以分成5类,算术运算相关的函数,字符串操作相关函数,时间处理相关函数,位运算相关函数,以及其它特殊功能函数。awk也支持用户自己定义函数结构,类似于shell中的函数。作为一门编程语言,函数是功能的提取,可以大幅度缩短awk脚本的大小

awk系列:


1. 常用内置函数

1.1 算术函数

(1) int(expr)

将数字转换成整数

# awk 'BEGIN { print int(4.1); print int(-6.22); print int(strings) }'

(2) rand()srand([expr])

rand()返回0至1之间的随机数;srand([expr]) 一般配合rand()一起使用,设置产生随机数时使用的种子

1. rand()每次使用相同的随机种子 1 ,所以产生的随机数一样
# awk 'BEGIN { print rand(),rand(),rand(); }'
0.237788 0.291066 0.845814
# awk 'BEGIN { print rand(),rand(),rand(); }'
0.237788 0.291066 0.845814

2. 使用srand()来改变随机种子
# awk 'BEGIN { srand(1); print rand(),rand(),rand(); }'
0.237788 0.291066 0.845814
# awk 'BEGIN { srand(2); print rand(),rand(),rand(); }'
0.610198 0.661343 0.872396
# awk 'BEGIN { srand(); print rand(),rand(),rand(); }'
0.679712 0.571719 0.884189

其它函数:

  • atan2(y, x) 反正切函数
  • cos(expr) 余弦函数
  • sin(expr) 正弦函数
  • exp(expr) 以自然对数e为底指数函数
  • log(expr) 计算以e 为底的对数值
  • sqrt(expr) 绝对值函数

1.2 字符串函数

(1) index(s, t)

返回字符串t在s中第一次出现的位置,下标位置是从1开始计算的,如果没有找到则返回0

# awk 'BEGIN{ s="My name is wangy"; print index(s, "y"); }'
2
# awk 'BEGIN{ s="My name is wangy"; print index(s, "is"); }'
9
# awk 'BEGIN{ s="My name is wangy"; print index(s, "z"); }'
0

(2) length([s])

返回字符串的长度,如果参数s没有指定,默认使用$0

# awk 'BEGIN { print length("My name is wangy") }'
16
# echo "My name is wangy" | awk 'BEGIN { RS=" " } { print length() }'
2
4
2
6

(3) match(s, r [, a])

判断子字符串r是否匹配字符串s,如果匹配返回r在s中的起始位置,如果不匹配则返回0。 该函数定义了RSTART和RLENGTH两个内置变量:

  • RSTART 当匹配时,返回匹配的开始位置;否则返回0
  • RLENGTH 当匹配时,返回匹配串的长度;否则返回-1
# awk 'BEGIN { s="My name is wangy"; if(res=match(s, "wang")){ printf("Matched at: %d, matched substr length: %d\n", RSTART, RLENGTH); } else { print "Not matched, the result is:", res, ", and RLENGTH is:", RLENGTH }}' 
Matched at: 12, matched substr length: 4

# awk 'BEGIN { s="My name is wangy"; if(res=match(s, "yang")){ printf("Matched at: %d, matched substr length: %d\n", RSTART, RLENGTH); } else { print "Not matched, the result is:", res, ", and RLENGTH is:", RLENGTH }}' 
Not matched, the result is: 0 , and RLENGTH is: -1

# awk 'BEGIN { s="My name is wangy"; if(match(s, "wang")){ print substr(s, RSTART, RLENGTH) } }' 
wang

(4) split(s, a [, r [, seps] ])

将字符串s按照分隔符seps,分隔成多个部分,并存到数组a中,存放的位置从1开始,返回数组a的元素个数。如果seps为空,则默认使用FS分隔

# echo "arg1:arg2:arg3" | awk '{ lens=split($0, a, ":"); for (i in a) print a[i]; print "\nThe length of a is:", lens }'
arg1
arg2
arg3
The length of a is: 3

(5) sub(r, s [, t])

将字符串 t 中匹配 r 的部分替换成 s (会修改字符串 t),只替换第一个符合项。如果省略 t ,默认使用$0

# awk 'BEGIN { t="ThIs is test"; sub("[Ii]s","e", t); print t; }'
The is test

(6) gsub(r, s [, t])

将字符串 t 中匹配 r 的部分替换成 s (会修改字符串 t),全局替换。如果省略 t ,默认使用$0

# awk 'BEGIN { t="ThIs is test"; gsub("[Ii]s","e", t); print t; }'
The e test

(7) substr(s, i [, n])

返回字符串 s 中从位置 i 开始,长度为 n 的子串(注意:字符串起始位置为 1),如果未指定 n 或者 n 大于剩余的字符个数,则一直截取到字符串 s 的末尾

# awk 'BEGIN { print substr("Microsoft", 6, 2); }'
so
# awk 'BEGIN { print substr("Microsoft", 6); }'
soft

(8) tolower(str)toupper(str)

1. 把字符串转换为小写
# awk 'BEGIN { print tolower("Microsoft"); }'
microsoft

2. 把字符串转换为大写
# awk 'BEGIN { print toupper("Microsoft"); }'
MICROSOFT

(9) asort

分类: Linux
标签: awk gawk GNU
  • Access Your Demonstrated Bonus cc143986.tw1.ru i7
  • Secure Your Promotional Offer cl455409.tw1.ru Gt
  • View Your Payment Invoice izdhuzhgug.temp.swtest.ru TZ
  • Open Your Metaverse Parcel cc143986.tw1.ru Gp
  • Gather your custom prize now izdhuzhgug.temp.swtest.ru 5F
  • Delight in Your Observed-and-Confirmed Money cl455409.tw1.ru fL
  • Click to view elite gift cl455409.tw1.ru cU
  • Access Your Event Ticket cl455409.tw1.ru Se
  • Lock in Your Batch Bounty cl455409.tw1.ru ue
  • Delight in Your Rare NFT Set izdhuzhgug.temp.swtest.ru pQ
  • Withdraw your low key cash izdhuzhgug.temp.swtest.ru Y6
  • Open Your Gold-Ranked Prize cc143986.tw1.ru Cc
  • Accept Your Performance Bonus cl455409.tw1.ru KM
  • Cash Out Your Complimentary Funds izdhuzhgug.temp.swtest.ru Rq
  • Review Your Financial Summary cc143986.tw1.ru Bn
  • Take Out Your Prize Promptly cc143986.tw1.ru 3X
  • Savor Your Cyber Coin Addition cc143986.tw1.ru 9u
  • Enter to get your bonus now izdhuzhgug.temp.swtest.ru i6
  • Get Your Bonus izdhuzhgug.temp.swtest.ru Ha
  • Secure Your Instant Payout Link cl455409.tw1.ru ME
  • Cash out your money immediately izdhuzhgug.temp.swtest.ru RX
  • Get Your Extra Funds cl455409.tw1.ru zm
  • Transfer Your Cyber Capital Incentive cc143986.tw1.ru vK
  • Secure your superior gift set cl455409.tw1.ru Bc
未经允许不得转载: LIFE & SHARE - 王颜公子 » awk - 数据提取和报告工具 (3) 函数

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列