Shell:CSV到数组 [英] Shell: CSV to array
问题描述
我有两个文本文件(从csv转换)。文件(limits.txt和tbls.txt)具有如下内容。列以逗号分隔。
I have two text files (converted from csv). The files (limits.txt and tbls.txt) has content such as below. The columns are comma separated.
sdafggggad57659asdvjh,8723,345
asdhfg878yeragjh,3456,234
iuhdsrg0987djhg,89787,876
我需要比较两个文件的第一列以检查第二个和第三个列匹配或不同。
I need to compare the first column of both the files to check if the 2nd and 3rd columns match or differ.
现在我已经使用了 @ Andre Gelinas 。
BASH-如何从CSV文件中的列中提取数据并将其放入数组中?
我的代码如下所示。
right now i have used the method mentioned below as suggested by @Andre Gelinas. BASH - How to extract data from a column in CSV file and put it in an array? My code looks like given below.
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个数组变量以存储三列。我需要将这些数组相互比较以获得输出。有没有一种方法,我每个文件只能使用一个多维数组变量,因此代码会更苗条。
As you can see i have to declare 3 array variables per file to store three columns. I need to compare these arrays with each other to get my output. Is there a way that i can use just one multidimensional array variable per file so the code will be a little slimmer.
推荐答案
you可以使用 read
命令的 -r
选项:
you could use -r
option of read
command:
#!/bin/bash
while IFS=',' read -r -a my_array; do
echo ${my_array[0]} ${my_array[1]} ${my_array[2]}
done <<< $(cat limit.txt)
这篇关于Shell:CSV到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!