脚本中的二维数组用法

在日常工作中,有时候分析日志时,常有需要用到二维数组的情况
一、shell中的二维数组用法(shell本事是只支持一维数组的)
利用eval的功能二次解析来实现二维数组
例子来自网上
arr1=(a b c)
arr2=(e f g)
arr3=(h i j)
如何通过二重循环输出上面三个数组上下并置而成的矩阵?代码如下
for ((i=0; i<3; i++)
do
  eval tmp=\${arr${i}[@]}
  for var in ${tmp}
  do
      echo -e "${var} \c"
  done
  echo  
done
输出的结果应该就是
a b c
e f g
h i j
在这个例子中内层循环直接用echo "${tmp}"当然更好。
二、awk中的二维数组使用
awk中的二维数组以至是多维数组array[index1,index2,……],其实是通过把多维下标通过固定分隔符SUBSEP拼接在一起,然后以一维数组的方式进行操作的。SUBSEP是数组下标分割符,默认为“\034”。可以事先设定SUBSEP,也可以直接在SUBSEP的位置输入你要用的分隔符。
1、数组赋值时可以直接使用array[i,j]=value的模式直接赋值
例:a[key1,key2]=value;
2、但是取值的时候就需要注意了,不能直接array[i,j]这种方式去使用。
例:awk 'BEGIN{SUBSEP=":";array["a","b"]=1;for(i in array) print i}'
    a:b
例:如果需要取值进行逻辑的话,需要先split一下。
for(item in a){split(item,s,SUBSEP);........
------------------------------------------------------------------
附注:
在SED and AWK中有关于awk多维数组的解释
file_array[2, 4]
This syntax does not create a multidimensional array. It is converted into a string that uniquely identifies the element in a linear array. The components of a multidimensional subscript are interpreted as individual strings ("2" and "4," for instance) and concatenated together separated by the value of the system variable   SUBSEP. The subscript-component separator is defined as  " \034 " by default, an unprintable character rarely found in ASCII text. Thus, awk maintains a one-dimensional array and the subscript for our previous example would actually be  "2\0344 " (the concatenation of " 2," the value of  SUBSEP, and " 4"). The main consequence of this simulation of multidimensional arrays is that the larger the array, the slower it is to access individual elements.
shell脚本中,bash虽然只支持一维数组,但是可以使用一些技巧来模拟二维数组的初始化。一个常见的方法是使用多个一维数组来表示二维数组的行和列。下面是一个示例代码,用于初始化一个二维数组: ```bash #!/bin/bash # 定义二维数组的行和列 rows=3 cols=3 # 初始化一个二维数组 declare -A array # 使用嵌套的循环来遍历数组,并为每个元素赋值 for ((i=0; i<rows; i++)) do for ((j=0; j<cols; j++)) do # 为数组的每个元素赋值 array[$i,$j]=$(( i + j )) done done # 打印二维数组的值 for ((i=0; i<rows; i++)) do for ((j=0; j<cols; j++)) do # 打印数组的每个元素 echo -n "${array[$i,$j]} " done echo done exit 0 ``` 以上代码使用嵌套的循环来遍历数组,并使用行和列的索引来给每个元素赋值。然后,通过再次遍历数组,打印出每个元素的值。这样就完成了一个简单的二维数组的初始化过程。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [shell脚本之模拟二维数组](https://blog.youkuaiyun.com/weixin_33853794/article/details/92091706)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [bash-sudoku:使用人类会使用的算法用 bash 编写的 Sodoku 解谜器](https://download.youkuaiyun.com/download/weixin_42100188/19300401)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值