Shell script - awk数组操作详细介绍
2014-04-05 20:28
387 查看
转自 /article/5147488.html
[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,就会自动创建改序列.
正确判断方法:
[chengmo@localhost ~]$ awk 'BEGIN{tB["a"]="a1";tB["b"]="b1";if( "c" in tB){print "ok";};for(k in tB){print k,tB[k];}}'
a a1
b b1
if(key in array) 通过这种方法判断数组中是否包含”key”键值。
[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,就会自动创建改序列.
正确判断方法:
[chengmo@localhost ~]$ awk 'BEGIN{tB["a"]="a1";tB["b"]="b1";if( "c" in tB){print "ok";};for(k in tB){print k,tB[k];}}'
a a1
b b1
if(key in array) 通过这种方法判断数组中是否包含”key”键值。
相关文章推荐
- [Linux]:linux awk数组操作详细介绍
- AWK数组操作详细学习----关联数组
- PHP数组循环操作详细介绍 附实例代码
- Js数组的操作push,pop,shift,unshift等方法详细介绍
- linux awk数组操作详细介绍
- Js数组的操作push,pop,shift,unshift等方法详细介绍
- linux awk数组操作详细介绍
- linux awk数组操作详细介绍
- linux awk数组操作详细介绍
- PHP数组循环操作详细介绍 附实例代码
- linux awk数组操作详细介绍
- linux awk数组操作详细介绍
- Js数组的操作push,pop,shift,unshift等方法详细介绍
- PHP数组循环操作详细介绍
- linux awk 数组操作详细介绍
- awk数组操作介绍
- AWK数组操作详细学习----关联数组
- Js数组的操作push,pop,shift,unshift等方法详细介绍
- ASP Cookies操作的详细介绍与实例代码
- linux awk 内置函数详细介绍(实例)