字符串函數
sub(regular expression,substitution string);
sub(regular expression,substitution string,target string);
[root@xieqichao ~]# awk '{sub("Tom","Tommy"); print}' employees #這裏使用Tommy替換了Tom。
Tommy Jones 4424 5/12/66 543354
當正則表達式Tom在第一個域中第一次被匹配後,
他將被字符串"Tommy"替換,如果將sub函數的第三個參數改爲$2,將不會有替換髮生。
[root@xieqichao ~]# awk '{sub("Tom","Tommy",$1); print}' employees
Tommy Jones 4424 5/12/66 543354
gsub(regular expression,substitution string);
gsub(regular expression,substitution string,target string);
和sub不同的是,如果第一個參數中正則表達式在記錄中出現多次,那麼gsub將完成多次替換,而sub只是替換第一次出現的。
index(string,substring)
該函數將返回第二個參數在第一個參數中出現的位置,偏移量從1開始。
[root@xieqichao ~]# awk 'BEGIN{print index("hello","el")}'
2
length(string)
該函數返回字符串的長度。
[root@xieqichao ~]# awk 'BEGIN{print length("hello")}'
5
substr(string,starting position)
substr(string,starting position,length of string)
該函數返回第一個參數的子字符串,其截取起始位置爲第二個參數(偏移量爲1),截取長度爲第三個參數,如果沒有該參數,則從第二個參數指定的位置起,直到string的末尾。
[root@xieqichao ~]# awk 'BEGIN{name = substr("Hello World",2,3); print name}'
ell
match(string,regular expression)
該函數返回在字符串中正則表達式位置的索引,如果找不到指定的正則表達式就返回0.match函數設置內置變量RSTART爲字符串中子字符串的開始位置,RLENGTH爲到字字符串末尾的字符個數。
[root@xieqichao ~]# awk 'BEGIN{start=match("Good ole CHINA", /[A-Z]+$/); print start}'
10
上例中的正則表達式[A-Z]+$表示在字符串的末尾搜索連續的大寫字母。在字符串"Good ole CHINA"的第10個位置找到字符串"CHINA"。
[root@xieqichao ~]# awk 'BEGIN{start=match("Good ole CHINA", /[A-Z]+$/); print RSTART, RLENGTH}'
10 5
RSTART表示匹配時的起始索引,RLENGTH表示匹配的長度。
[root@xieqichao ~]# awk 'BEGIN{string="Good ole CHINA";\
start=match(string, /[A-Z]+$/); print substr(string,RSTART, RLENGTH)}'
CHINA
這裏將match、RSTART、RLENGTH和substr巧妙的結合起來了。
toupper(string)
tolower(string)
以上兩個函數分別返回參數字符串的大寫和小寫的形式。
[root@xieqichao ~]# awk 'BEGIN {print toupper("hello"); print tolower("WORLD")}'
HELLO
world
split(string,array,field seperator)
split(string,array)
該函數使用作爲第三個參數的域分隔符把字符串分隔爲一個數組。如果第三個參數沒有提供,則使用當前默認的FS值。
[root@xieqichao ~]# awk 'BEGIN{split("11/20/2011",date,"/"); print date[2]}'
20
variable = sprintf("string with format specifiers ",expr1,expr2,...)
該函數和printf的差別等同於C語言中printf和sprintf的差別。前者將格式化後的結果輸出到輸出流,而後者輸出到函數的返回值中。
[root@xieqichao ~]# awk 'BEGIN{line = sprintf("%-15s %6.2f ", "hello",4.2); print line}'
hello 4.20
時間函數:
systime()
該函數返回當前時間距離1970年1月1日之間相差的秒數。
[root@xieqichao ~]# awk 'BEGIN{print systime()}'
1321369554
strftime()
時間格式化函數,其格式化規則等同於C語言中的strftime函數提供的規則,見以下列表:
數據格式 | 含義 |
---|---|
%a | Abbreviated weekday name |
%A | Full weekday name |
%b | Abbreviated month name |
%B | Full month name |
%c | Date and time representation appropriate for locale |
%d | Day of month as decimal number (01 – 31) |
%H | Hour in 24-hour format (00 – 23) |
%I | Hour in 12-hour format (01 – 12) |
%j | Day of year as decimal number (001 – 366) |
%m | Month as decimal number (01 – 12) |
%M | Minute as decimal number (00 – 59) |
%p | Current locale’s A.M./P.M. indicator for 12-hour clock |
%S | Second as decimal number (00 – 59) |
%U | Week of year as decimal number, with Sunday as first day of week (00 – 53) |
%w | Weekday as decimal number (0 – 6; Sunday is 0) |
%W | Week of year as decimal number, with Monday as first day of week (00 – 53) |
%x | Date representation for current locale |
%X | Time representation for current locale |
%y | Year without century, as decimal number (00 – 99) |
%Y | Year with century, as decimal number |
[root@xieqichao ~]# awk 'BEGIN{ print strftime("%D",systime())}'
11/15/11
[root@xieqichao ~]# awk 'BEGIN{ now = strftime("%T"); print now}'
23:17:29
內置數學函數:
名稱 | 返回值 |
---|---|
atan2(x,y) | y,x範圍內的餘切 |
cos(x) | 餘弦函數 |
exp(x) | 求冪 |
int(x) | 取整 |
log(x) | 自然對數 |
sin(x) | 正弦函數 |
sqrt(x) | 平方根 |
[root@xieqichao ~]# awk 'BEGIN{print 31/3}'
10.3333
[root@xieqichao ~]# awk 'BEGIN{print int(31/3)}'
10
自定義函數:
自定義函數可以放在awk腳本的任何可以放置模板和動作的地方。
function name(parameter1,parameter2,...) {
statements
return expression
}
給函數中本地變量傳遞值。只使用變量的拷貝。數組通過地址或者指針傳遞,所以可以在函數內部直接改變數組元素的值。函數內部使用的任何沒有作爲參數傳遞的變量都被看做是全局變量,也就是這些變量對於整個程序都是可見的。如果變量在函數中發生了變化,那麼就是在整個程序中發生了改變。唯一向函數提供本地變量的辦法就是把他們放在參數列表中,這些參數通常被放在列表的最後。如果函數調用沒有提供正式的參數,那麼參數就初始化爲空。return語句通常就返回程序控制並向調用者返回一個值。
[root@xieqichao ~]# cat grades
20 10
30 20
40 30
[root@xieqichao ~]# cat add.sc
function add(first,second) {
return first + second
}
{ print add($1,$2) }
[root@xieqichao ~]# awk -f add.sc grades
30
50
70