AWK許多內(nèi)置函數(shù),隨時(shí)可為程序員使用。本教程介紹了AWK的算術(shù),字符串,時(shí)間,位操作和其他雜項(xiàng)函數(shù)的例子:
AWK具有以下內(nèi)置的算術(shù)函數(shù):
它返回弧度的反正切(y/x) 。下面簡單的例子說明了這一點(diǎn):
[jerry]$ awk 'BEGIN {
PI = 3.14159265
x = -10
y = 10
result = atan2 (y,x) * 180 / PI;
printf "The arc tangent for (x=%f, y=%f) is %f degrees\n", x, y, result
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
The arc tangent for (x=-10.000000, y=10.000000) is 135.000000 degrees
此函數(shù)返回expr的余弦(以弧度形式)。下面簡單的例子說明了這一點(diǎn):
[jerry]$ awk 'BEGIN {
PI = 3.14159265
param = 60
result = cos(param * PI / 180.0);
printf "The cosine of %f degrees is %f.\n", param, result
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
The cosine of 60.000000 degrees is 0.500000.
此函數(shù)被用于找到指數(shù)值。
[jerry]$ awk 'BEGIN {
param = 5
result = exp(param);
printf "The exponential value of %f is %f.\n", param, result
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
The exponential value of 5.000000 is 148.413159.
這個(gè)函數(shù)截?cái)鄀xpr為整數(shù)值。下面簡單的例子說明了這一點(diǎn):
[jerry]$ awk 'BEGIN {
param = 5.12345
result = int(param)
print "Truncated value =", result
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Truncated value = 5
此函數(shù)計(jì)算的自然對(duì)數(shù)。
[jerry]$ awk 'BEGIN {
param = 5.5
result = log (param)
printf "log(%f) = %f\n", param, result
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
log(5.500000) = 1.704748
該函數(shù)返回一個(gè)隨機(jī)數(shù)N,在0和1之間,使得0<= N <1。例如下面的例子會(huì)產(chǎn)生三個(gè)隨機(jī)數(shù):
[jerry]$ awk 'BEGIN {
print "Random num1 =" , rand()
print "Random num2 =" , rand()
print "Random num3 =" , rand()
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Random num1 = 0.237788 Random num2 = 0.291066 Random num3 = 0.845814
此函數(shù)返回expr的正弦(以弧度形式)。下面簡單的例子說明了這一點(diǎn):
[jerry]$ awk 'BEGIN {
PI = 3.14159265
param = 30.0
result = sin(param * PI /180)
printf "The sine of %f degrees is %f.\n", param, result
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
The sine of 30.000000 degrees is 0.500000.
該函數(shù)返回expr的平方根。
[jerry]$ awk 'BEGIN {
param = 1024.0
result = sqrt(param)
printf "sqrt(%f) = %f\n", param, result
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
sqrt(1024.000000) = 32.000000
這個(gè)函數(shù)使用產(chǎn)生種子值的隨機(jī)數(shù)。它使用expr作為隨機(jī)數(shù)生成的新的種子。如果沒有expr,它使用一天的時(shí)間值作為種子值。
[jerry]$ awk 'BEGIN {
param = 10
printf "srand() = %d\n", srand()
printf "srand(%d) = %d\n", param, srand(param)
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
srand() = 1 srand(10) = 1417959587
AWK具有以下內(nèi)置字符串函數(shù):
這個(gè)函數(shù)排序arr,使用gawk的常規(guī)規(guī)則比較值的內(nèi)容,并替換排序值的索引常用使用連續(xù)整數(shù)是從1開始。
[jerry]$ awk 'BEGIN {
arr[0] = "Three"
arr[1] = "One"
arr[2] = "Two"
print "Array elements before sorting:"
for (i in arr) {
print arr[i]
}
asort(arr)
print "Array elements after sorting:"
for (i in arr) {
print arr[i]
}
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Array elements before sorting: Three One Two Array elements after sorting: One Three Two
此函數(shù)的行為類似于asort(),所不同的是數(shù)組索引用于排序。
[jerry]$ awk 'BEGIN {
arr["Two"] = 1
arr["One"] = 2
arr["Three"] = 3
asorti(arr)
print "Array indices after sorting:"
for (i in arr) {
print arr[i]
}
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Array indices after sorting: One Three Two
gsub代表全局替換。它用正則表達(dá)式分每個(gè)匹配。第三個(gè)參數(shù)是可選的,如果省略它,那么$0被使用。
[jerry]$ awk 'BEGIN {
str = "Hello, World"
print "String before replacement = " str
gsub("World", "Jerry", str)
print "String after replacement = " str
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
String before replacement = Hello, World String after replacement = Hello, Jerry
它檢查sub是否是str的子字符串。如果成功則返回sub開始位置,否則返回0。str第一個(gè)字符的位置是1。
[jerry]$ awk 'BEGIN {
str = "One Two Three"
subs = "Two"
ret = index(str, subs)
printf "Substring \"%s\" found at %d location.\n", subs, ret
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Substring "Two" found at 5 location.
它返回字符串字符串的長度。
[jerry]$ awk 'BEGIN {
str = "Hello, World !!!"
print "Length = ", length(str)
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Length = 16
它返回正則表達(dá)式的字符串str第一個(gè)最長的匹配索引。如果沒有找到匹配返回0。
[jerry]$ awk 'BEGIN {
str = "One Two Three"
subs = "Two"
ret = match(str, subs)
printf "Substring \"%s\" found at %d location.\n", subs, ret
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Substring "Two" found at 5 location.
這個(gè)函數(shù)分割字符串str為正則表達(dá)式regex字段,字段被加載到數(shù)組arr。如果省略regex那么fs被使用。
[jerry]$ awk 'BEGIN {
str = "One,Two,Three,Four"
split(str, arr, ",")
print "Array contains following values"
for (i in arr) {
print arr[i]
}
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Array contains following values One Two Three Four
該函數(shù)返回按照expr-list格式構(gòu)造一個(gè)字符串。
[jerry]$ awk 'BEGIN {
str = sprintf("%s", "Hello, World !!!")
print str
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Hello, World !!!
這個(gè)函數(shù)檢查str并返回它的數(shù)值。如果str以0開始,把它當(dāng)作一個(gè)八進(jìn)制數(shù)。如果str開頭是0x或0X,那么它當(dāng)作一個(gè)十六進(jìn)制數(shù)。否則,假設(shè)它是一個(gè)十進(jìn)制數(shù)。
[jerry]$ awk 'BEGIN {
print "Decimal num = " strtonum("123")
print "Octal num = " strtonum("0123")
print "Hexadecimal num = " strtonum("0x123")
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Decimal num = 123 Octal num = 83 Hexadecimal num = 291
這個(gè)函數(shù)執(zhí)行單一的替代。它用正則表達(dá)式子第一次出現(xiàn)。第三個(gè)參數(shù)是可選的,如果它被刪去,$0被使用。
[jerry]$ awk 'BEGIN {
str = "Hello, World"
print "String before replacement = " str
sub("World", "Jerry", str)
print "String after replacement = " str
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
String before replacement = Hello, World String after replacement = Hello, Jerry
該函數(shù)返回字符串str的子字符串,起始于長度l為索引開始。如果省略長度,則返回str的后綴為索引起始。
[jerry]$ awk 'BEGIN {
str = "Hello, World !!!"
subs = substr(str, 1, 5)
print "Substring = " subs
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Substring = Hello
該函數(shù)返回字符串str具有轉(zhuǎn)換為小寫全部大寫字符的副本。
[jerry]$ awk 'BEGIN {
str = "HELLO, WORLD !!!"
print "Lowercase string = " tolower(str)
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Lowercase string = hello, world !!!
該函數(shù)返回字符串str具有轉(zhuǎn)換為大寫小寫字符的副本。
[jerry]$ awk 'BEGIN {
str = "hello, world !!!"
print "Uppercase string = " toupper(str)
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Uppercase string = HELLO, WORLD !!!
AWK擁有的內(nèi)置時(shí)間函數(shù)如下:
該函數(shù)返回當(dāng)天的當(dāng)前時(shí)間以來的大紀(jì)元(1970-01-0100:00:00 UTC在POSIX系統(tǒng))的秒數(shù)。
[jerry]$ awk 'BEGIN {
print "Number of seconds since the Epoch = " systime()
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Number of seconds since the Epoch = 1418574432
返回由systime()這個(gè)函數(shù)轉(zhuǎn)換的timespec字符串進(jìn)入相同的形式的時(shí)間標(biāo)記。所述的timespec形式如YYYY MM DD HH MM SS的字符串。
[jerry]$ awk 'BEGIN {
print "Number of seconds since the Epoch = " mktime("2014 12 14 30 20 10")
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Number of seconds since the Epoch = 1418604610
根據(jù)格式規(guī)范此函數(shù)格式化時(shí)間戳。
[jerry]$ awk 'BEGIN {
print strftime("Time = %m/%d/%Y %H:%M:%S", systime())
}'
在執(zhí)行上面的代碼后,得到以下結(jié)果:
Time = 12/14/2014 22:08:42
以下是由AWK支持的各種時(shí)間格式:
| 日期格式規(guī)范 | 描述 |
|---|---|
| %a | The locale’s abbreviated weekday name. |
| %A | The locale’s full weekday name. |
| %b | The locale’s abbreviated month name. |
| %B | The locale’s full month name. |
| %c | The locale’s appropriate date and time representation. (This is %A %B %d %T %Y in the C locale.) |
| %C | The century part of the current year. This is the year divided by 100 and truncated to the next lower integer. |
| %d | The day of the month as a decimal number (01–31). |
| %D | Equivalent to specifying %m/%d/%y. |
| %e | The day of the month, padded with a space if it is only one digit. |
| %F | Equivalent to specifying %Y-%m-%d. This is the ISO 8601 date format. |
| %g | The year modulo 100 of the ISO 8601 week number, as a decimal number (00–99). For example, January 1, 1993 is in week 53 of 1992. Thus, the year of its ISO 8601 week number is 1992, even though its year is 1993. Similarly, December 31, 1973 is in week 1 of 1974. Thus, the year of its ISO week number is 1974, even though its year is 1973. |
| %G | The full year of the ISO week number, as a decimal number. |
| %h | Equivalent to %b. |
| 上一篇:AWK輸出重定向下一篇:AWK數(shù)組 |