外壳:CSV 到数组



我有两个文本文件(从csv转换而来(。文件(限制.txt和 tbls.txt(包含如下内容。列以逗号分隔。

sdafggggad57659asdvjh,8723,345
asdhfg878yeragjh,3456,234
iuhdsrg0987djhg,89787,876

我需要比较两个文件的第一列,以检查第二列和第三列是否匹配或不同。

现在,我已经使用了@Andre Gelinas建议的下面提到的方法。 BASH - 如何从CSV文件中的列中提取数据并将其放入数组中? 我的代码如下所示。

limit_t=( $(cut -d "," -f1 limits.txt))
limit_r=( $(cut -d "," -f2 limits.txt))
limit_w=( $(cut -d "," -f3 limits.txt))
tbls_t=( $(cut -d "," -f1 tbls.txt))
tbls_r=( $(cut -d "," -f2 tbls.txt))
tbls_w=( $(cut -d "," -f3 tbls.txt))

如您所见,我必须为每个文件声明 3 个数组变量来存储三列。我需要将这些数组相互比较才能获得输出。有没有办法我可以为每个文件只使用一个多维数组变量,这样代码就会更细一些。

您可以使用read命令-r选项:

#!/bin/bash
while IFS=',' read -r -a my_array; do
echo ${my_array[0]} ${my_array[1]} ${my_array[2]}
done <<< $(cat limit.txt)

最新更新