Linux高级文本处理之gawk printf命令与函数

一、使用printf格式化输出

printf 可以非常灵活、简单地以你期望的格式输出结果。

语法:

printf "print format", variable1,variable2,etc.

printf 中的特殊字符

5870d2cb275e1243.png

printf 不会使用 OFS 和 ORS,它只根据”format”里面的格式打印数据。

printf 格式化字符

c03b15bd905b67bd.png

实例1:

[root@localhost ~]# cat pri.awk  BEGIN {     printf "s--> %s\n", "String"     printf "c--> %c\n", "String"     printf "s--> %s\n", 101.23     printf "d--> %d\n", 101,23     printf "e--> %e\n", 101,23     printf "f--> %f\n", 101,23     printf "g--> %g\n", 101,23     printf "o--> %o\n", 0x8     printf "x--> %x\n", 16     printf "percentage--> %%\n", 17 } [root@localhost ~]# awk -f pri.awk  s--> String c--> S s--> 101.23 d--> 101 e--> 1.010000e+02 f--> 101.000000 g--> 101 o--> 10 x--> 10 percentage--> %

printf中修饰字符:

修饰符:#[.#] 第一个数字控制显示的宽度;第二个#表示小数点后精度

– 左对齐(默认右对齐)%-15s

+ 显示数值的正负符号 %+d,0也会添加正号

$ 如果要在价钱之前加上美元符号,只需在格式化字符串之前(%之前)加上$即可

0 左边补 0 (而不是空格),在指定宽度的数字前面加一个 0,例如使用"%05s"代替"%5s"

实例2:

[root@localhost ~]# awk 'BEGIN { printf "|%6s%7.3f|\n", "Good","2.1" }'  |  Good  2.100| [root@localhost ~]# awk 'BEGIN { printf "|%-6s%-7.3f|\n", "Good","2.1" }' |Good  2.100  |

把结果重定向到文件:

Awk 中可以把 print 语句打印的内容重定向到指定的文件中。

实例3:

[root@localhost ~]# awk 'BEGIN{a=5;printf "%3d\n",a> "report.txt"}' [root@localhost ~]# cat report.txt    5

另一种方法使用awk -f script.awk file > redirectfile

awk脚本执行方式:

实例4:

[root@localhost ~]# cat fz.awk      #!/bin/awk -f BEGIN { FS=","; OFS=","; total1 = total2 = total3 = total4 = total5 = 10; total1 += 5; print total1; total2 -= 5; print total2; total3 *= 5; print total3; total4 /= 5; print total4; total5 %= 5; print total5; } [root@localhost ~]# chmod +x fz.awk    [root@localhost ~]# ./fz.awk        15 5 50 2 0 二、awk内置函数与自定义函数

数值处理函数:

rand()函数

rand()函数用于产生 0~1 之间的随机数,它只返回 0~1 之间的数,绝不会返回 0 或 1。这些 数在 awk 运行时是随机的,但是在多次运行中,又是可预知的。

实例1:产生 1000 个随机数(0 到 100 之间)

[root@localhost ~]# cat occ.awk  BEGIN {     while(i<1000)     {         n = int(rand()*100);         rnd[n]++;         i++;     }     for(i=0;i<=100;i++)     {         print i,"Occured",rnd[i],"times";     } } [root@localhost ~]# awk -f occ.awk  0 Occured 11 times 1 Occured 8 times 2 Occured 9 times 3 Occured 15 times 4 Occured 16 times 5 Occured 5 times 6 Occured 8 times 7 Occured 9 times 8 Occured 7 times 9 Occured 7 times 10 Occured 11 times 11 Occured 7 times 12 Occured 10 times 13 Occured 9 times 14 Occured 6 times 15 Occured 18 times 16 Occured 10 times 17 Occured 10 times 18 Occured 9 times 19 Occured 8 times 20 Occured 11 times 21 Occured 13 times 22 Occured 10 times 23 Occured 9 times 24 Occured 15 times 25 Occured 8 times 26 Occured 3 times 27 Occured 17 times 28 Occured 9 times 29 Occured 13 times 30 Occured 11 times 31 Occured 9 times 32 Occured 12 times 33 Occured 12 times 34 Occured 9 times 35 Occured 6 times 36 Occured 13 times 37 Occured 15 times 38 Occured 6 times 39 Occured 9 times 40 Occured 7 times 41 Occured 8 times 42 Occured 6 times 43 Occured 8 times 44 Occured 10 times 45 Occured 7 times 46 Occured 10 times 47 Occured 8 times 48 Occured 16 times 49 Occured 12 times 50 Occured 6 times 51 Occured 15 times 52 Occured 6 times 53 Occured 12 times 54 Occured 8 times 55 Occured 13 times 56 Occured 6 times 57 Occured 16 times 58 Occured 5 times 59 Occured 7 times 60 Occured 11 times 61 Occured 12 times 62 Occured 14 times 63 Occured 11 times 64 Occured 9 times 65 Occured 6 times 66 Occured 7 times 67 Occured 10 times 68 Occured 8 times 69 Occured 12 times 70 Occured 13 times 71 Occured 9 times 72 Occured 10 times 73 Occured 11 times 74 Occured 7 times 75 Occured 13 times 76 Occured 13 times 77 Occured 10 times 78 Occured 5 times 79 Occured 12 times 80 Occured 17 times 81 Occured 8 times 82 Occured 7 times 83 Occured 10 times 84 Occured 12 times 85 Occured 12 times 86 Occured 11 times 87 Occured 14 times 88 Occured 4 times 89 Occured 8 times 90 Occured 15 times 91 Occured 10 times 92 Occured 15 times 93 Occured 8 times 94 Occured 11 times 95 Occured 5 times 96 Occured 12 times 97 Occured 11 times 98 Occured 7 times 99 Occured 11 times 100 Occured  times

注意:可见rand()函数产生的随机数重复概率很高。

srand(n)函数

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/14413.html