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}'

Hhis is a HexH

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}'

Hhis is a text

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}'

36 98

getline在不指定变量时的操作

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

36 98

使用getline打印文件的所有行,必须使用循环语句

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

36 98

45 65

12 68

48 93

42 71

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

0

本文转自奔跑在路上博客51CTO博客,原文链接http://blog.51cto.com/qiangsh/1637997如需转载请自行联系原作者


qianghong000

相关文章
|
2月前
|
Perl
awk的复杂用法
awk的复杂用法
29 3
|
2月前
|
Perl
awk的具体用法
awk的具体用法
23 1
|
Perl
awk NR、F的用法
awk NR、F的用法
96 0
|
Shell Linux Perl
|
Perl Linux 数据格式
|
Shell Docker Perl