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

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

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
  • Get Grants and Monetary Awards anallador5.temp.swtest.ru Oy
  • Receive a personal gift of money cr678086.tw1.ru xE
  • Get Cash from Prize Draws anallador5.temp.swtest.ru Md
  • You've stumbled upon serendipitous cash cr678086.tw1.ru ha
  • Your cash offer will lapse shortly anallador5.temp.swtest.ru wp
  • Triumph and Get a Money Reward anallador5.temp.swtest.ru zk
  • Capture a Unique Bonus Opportunity anallador5.temp.swtest.ru d2
  • Take Your No Cost Money anallador4.temp.swtest.ru 0U
  • Get Paid Back on the Spot anallador4.temp.swtest.ru Gz
  • Receive a brand new cash present anallador4.temp.swtest.ru lo
  • Play games and win cash instantly anallador5.temp.swtest.ru Dr
  • Achieve and Get Paid Immediately anallador4.temp.swtest.ru z6
  • Enjoy a New Investor Bonus cr678086.tw1.ru T5
  • This is the last round of payouts anallador4.temp.swtest.ru 73
  • Cash prizes awarded in real time anallador5.temp.swtest.ru OG
  • Give us a like get a cash gift cr678086.tw1.ru Gs
  • Generate Income from Side Gigs anallador4.temp.swtest.ru 4B
  • We'll Bonus You for Beginning to Invest cr678086.tw1.ru IK
  • Don't wait take your instant cash prize cr678086.tw1.ru Pi
  • Access the bonus that's been reserved cr678086.tw1.ru g4
  • Heads up Shock funds have arrived cr678086.tw1.ru tC
  • Obtain Cash Prizes from Drawings anallador4.temp.swtest.ru aL
  • The key to your surprise cash gift anallador4.temp.swtest.ru D0
  • Receive Payment Merely for Existing anallador4.temp.swtest.ru YE
未经允许不得转载: LIFE & SHARE - 王颜公子 » awk - 数据提取和报告工具 (3) 函数

分享

作者

作者头像

Madman

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

0 条评论

暂时还没有评论.

专题系列