0

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.

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))

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.

4
  • @AndreGelinas i have added more information. I hope i am able to explain what i am looking for. Let me know if this can be achieved. Commented Oct 9, 2018 at 7:33
  • So what's the target here? I need to compare the first column of both the files to check if the 2nd and 3rd columns match or differ. What do you need in the result? Array of ['matched', 'differ', 'matched']? Or keyed object with the first column as a key? As I understand, you need to get two associative arrays with two columns as a content and compare it to another associative array in a loop. Is it correct? Commented Oct 9, 2018 at 7:45
  • So both the files have same structure... First column has table name, second has read value and 3rd has write value... The limits.txt file is static and has table name and celling values for read and write... The tbls.txt has table name and CURRENT read, write values... I need to compare both with table names which is first column and then check current read, write values of respective tables with celling limits to check if they have hit limits or not... Commented Oct 9, 2018 at 7:59
  • Bash doesn't support multidimensional arrays, but ksh does. That said, in ksh I would use compound variables instead with an associative array. I mean no offense here, but it's starting to look as an XY problem as you don't need to create arrays for tbls.txt. You only need to load limits.txt, static, as an array an iterate through tbls.txt, dynamic, to see if the values has hit the limits and act accordingly. Commented Oct 9, 2018 at 12:33

1 Answer 1

0

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.