设为首页 加入收藏

TOP

Linux awk数组操作详细介绍
2014-11-24 07:34:50 来源: 作者: 【 】 浏览:1
Tags:Linux awk 操作 详细 介绍

用awk进行文本处理,少不了就是它的数组处理。那么awk数组有那些特点,一般常见运算又会怎么样呢。我们先看下下面的一些介绍,结合例子我们会讲解下它的不同之处。在 awk 中数组叫做关联数组(associative arrays),因为下标记可以是数也可以是串。awk 中的数组不必提前声明,也不必声明大小。数组元素用 0 或空串来初始化,这根据上下文而定。例如:


一、定义方法


1:可以用数值作数组索引(下标)


Tarray[1]=“cheng mo”Tarray[2]=“800927”


2:可以用字符串作数组索引(下标)


Tarray[“first”]=“cheng ”Tarray[“last”]=”mo” Tarray[“birth”]=”800927”


使用中 print Tarray[1] 将得到”cheng mo” 而 print Tarray[2] 和 print[“birth”] 都将得到 ”800927” 。


二、数组相关函数


[chengmo@localhost ~]$ awk --version
GNU Awk 3.1.5


使用版本是:3.1以上,不同版本下面函数不一定相同


得到数组长度(length方法使用)


[chengmo@localhost ~]$ awk 'BEGIN{info="it is a test";lens=split(info,tA," ");print length(tA),lens;}'
4 4


length返回字符串以及数组长度,split进行分割字符串为数组,也会返回分割得到数组长度。


(asort使用):


[chengmo@localhost ~]$ awk 'BEGIN{info="it is a test";split(info,tA," ");print asort(tA);}'
4


asort对数组进行排序,返回数组长度。


输出数组内容(无序,有序输出):


[chengmo@localhost ~]$ awk 'BEGIN{info="it is a test";split(info,tA," ");for(k in tA){print k,tA[k];}}'
4 test
1 it
2 is
3 a


for…in 输出,因为数组是关联数组,默认是无序的。所以通过for…in 得到是无序的数组。如果需要得到有序数组,需要通过下标获得。


[chengmo@localhost ~]$ awk 'BEGIN{info="it is a test";tlen=split(info,tA," ");for(k=1;k<=tlen;k++){print k,tA[k];}}'
1 it
2 is
3 a
4 test


注意:数组下标是从1开始,与c数组不一样。


判断键值存在以及删除键值:


一个错误的判断方法:


[chengmo@localhost ~]$ awk 'BEGIN{tB["a"]="a1";tB["b"]="b1";if(tB["c"]!="1"){print "no found";};for(k in tB){print k,tB[k];}}'
no found
a a1
b b1
c


以上出现奇怪问题,tB[“c”]没有定义,但是循环时候,发现已经存在该键值,它的值为空,这里需要注意,awk数组是关联数组,只要通过数组引用它的key,就会自动创建改序列.


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux程序源码编译安装的configur.. 下一篇Linux awk 内置变量使用介绍

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)