天天看點

AWK函數

函數

内置字元串函數表

概述

gsub(r,s)

用s替換r(文本整頁)

gsub(r,s,f)

在f中用s替換r(文本整頁)  

sub(r,s)

s替換r (單位元組)  

sub(r,s,f)

在f中用s替換r(但位元組)  

match(r,s)

傳回字元串r是否包涵s的字元串

index(r,s)

傳回r字元串中s的位置

length(s)

傳回字元串s的長度

split(r,s,“fs”)

在fs上将r分成序列s(第幾列,數組名,分隔符)

substr(r,s,f)

在r上截取s到f的字元串  

tolower(r)

在r字元串中将所有大寫字元更改為小寫

toupper(r)

在r字元串中将所有小寫字元更改為大寫

asort(s,r)

對s數組的值進行排序,并丢棄s的值,賦予b

asorti(s,r)

對數組的下表進行排序,并丢去原來的值

gensub(s,r,f)

指定f的字段數,r替換s

getline [ Variable ] < Expression

将檔案内容放入到指定變量,并列印,但每次隻讀取一行

getline [ variable ]

如果沒有指定變量,則$0成為它的值

system()

在awk中調用linux系統指令

gsub函數主要用于在替換整個文本的字元串

[root@localhost ~]# echo "this is a text" | awk'{gsub("t","H");print $0}'

Hhis is a HexH

gsub也可以将字元串定義一個變量,然後在進行替換

[root@localhost ~]# awk 'BEGIN{a="this is atext";gsub("t","H",a);print a}'

sub替換字元串中的單個第一次出現的字元

[root@localhost ~]# echo "this is a text" | awk'{sub("t","H");print $0}'

Hhis is a text

sub定義變量後替換。

[root@localhost ~]# awk 'BEGIN{a="this is atext";sub("t","H",a);print a}'

match主要用于在測試字元串中是否包含s

[root@localhost ~]# awk 'BEGIN{a="this is atext";s=match(a,"x");print s}'

13

index傳回r在s中的位置,注意的就是隻比對一次

[root@localhost ~]# awk 'BEGIN{a="this is atext";s=index(a,"t");print s}'

1

length傳回字元串的長度

[root@localhost ~]# awk 'BEGIN{a="this is atext";s=length(a);print s}'

14

split在fs上将r分成序列s(第幾列,數組名,分隔符)

[root@localhost ~]# awk 'BEGIN{a="this is atext";split(a,c," ");for(i in c){print i,c[i]}}' | sort

1 this

2 is

3 a

4 text

substr在r上截取s到f的字元串

[root@localhost ~]# awk 'BEGIN{a="this is atext";s=substr(a,"1","11");print s }'

this is a t

substr冒上空格也算一個字元,下面來個去除空格,用到gsub

[root@localhost ~]# awk 'BEGIN{a="this is atext";gsub("","",a);s=substr(a,"1","11");print s }'

thisisatext

tolower将所有字元串由大寫轉換成小寫

[root@localhost ~]# awk 'BEGIN{a="THIS IS ATEXT";print tolower(a)}'

this is a text

toupper将所有字元串由小寫轉換成大寫

[root@localhost ~]# awk 'BEGIN{a="this is atext";print toupper(a)}'

THIS IS A TEXT

asort對數組的值進行排序,丢棄原先的值,必須定義數組的值

===== 文本内容 =====

36 98

45 65

12 68

48 93

42 71

[root@localhost ~]# awk'{a[$1]=$2}END{s=asort(a,b);for(i=1;i<=s;i++)print i,b[i]}' cc

1 65

2 68

3 71

4 93

5 98

asorti對數組的下标進行排序,這裡沒有給數組定義

[root@localhost ~]# awk'{a[$1]}END{s=asorti(a,b);for(i=1;i<=s;i++)print i,b[i]}' cc

1 12

2 36

3 42

4 45

5 48

gensub指定要替換的單個字元串。這個函數比較舒服看下面的用法

[root@localhost ~]# echo '111111' | awk '{printgensub("1","2",6)}'

111112

getline預設讀取檔案一行,如果不沒指定BEGIN,則列印第二行,因為awk在執行的時候已經讀取了一行了

[root@localhost www]# awk 'BEGIN{getline d <"cc" ;print d}'

getline在不指定變量時的操作

[root@localhost www]# awk 'BEGIN{getline <"cc" ;print $0}'

使用getline列印檔案的所有行,必須使用循環語句

[root@localhost www]# awk 'BEGIN{while(getline d <"cc"){print d}}'

system函數用來調用系統中的指令

[root@localhost www]# awk 'BEGIN{a=system("ls-l");print a}'

total 20

-rw-r--r--. 1 root root  30 Jan  8 12:02 cc

drwxr-xr-x. 2 root root 4096 Feb 14  2012 cgi-bin

drwxr-xr-x. 3 root root 4096 Nov  1 15:20 error

drwxr-xr-x. 2 root root 4096 Feb 14  2012 html

drwxr-xr-x. 3 root root 4096 Nov  1 15:20 icons

本文轉自奔跑在路上部落格51CTO部落格,原文連結http://blog.51cto.com/qiangsh/1637997如需轉載請自行聯系原作者

qianghong000

下一篇: 箭頭函數

繼續閱讀