歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux awk 內置函數詳細介紹(實例)

Linux awk 內置函數詳細介紹(實例)

日期:2017/2/28 14:56:20   编辑:Linux教程

這節詳細介紹awk內置函數,主要分以下3種類似:算數函數、字符串函數、其它一般函數、時間函數

一、算術函數:

以下算術函數執行與 C 語言中名稱相同的子例程相同的操作:

函數名 說明 atan2( y, x ) 返回 y/x 的反正切。 cos( x ) 返回 x 的余弦;x 是弧度。 sin( x ) 返回 x 的正弦;x 是弧度。 exp( x ) 返回 x 冪函數。 log( x ) 返回 x 的自然對數。 sqrt( x ) 返回 x 平方根。 int( x ) 返回 x 的截斷至整數的值。 rand( ) 返回任意數字 n,其中 0 <= n < 1。 srand( [Expr] ) 將 rand 函數的種子值設置為 Expr 參數的值,或如果省略 Expr 參數則使用某天的時間。返回先前的種子值。

舉例說明:

[chengmo@CentOS5 ~]$ awk 'BEGIN{OFMT="%.3f";fs=sin(1);fe=exp(10);fl=log(10);fi=int(3.1415);print fs,fe,fl,fi;}'
0.841 22026.466 2.303 3

OFMT 設置輸出數據格式是保留3位小數

獲得隨機數:

[chengmo@centos5 ~]$ awk 'BEGIN{srand();fr=int(100*rand());print fr;}'
78
[chengmo@centos5 ~]$ awk 'BEGIN{srand();fr=int(100*rand());print fr;}'
31
[chengmo@centos5 ~]$ awk 'BEGIN{srand();fr=int(100*rand());print fr;}'

41

Copyright © Linux教程網 All Rights Reserved