I currently am working with a matrix in bash. I have, say, a 2x4 matrix inside a file:
1 2 3 4
5 6 7 8
I have read from this file, and have stored all of these elements inside an array, such as:
my_arr={1 2 3 4 5 6 7 8}
Next, I piped my echo output so that the spaces change to tabs:
echo ${my_arr[@]} | tr ' ' '\t'
**output**:
my_arr={1 2 3 4 5 6 7 8}
My question now is, I want to have a NEW-LINE after every four elements printed; in other words, is it possible for me to print out the array line-by-line, or row-by-row?
EDIT Here is what I have in my actual code:
array=()
cols #This contains number of columns
while read line1 <&3
do
for i in $line1
do
array+=($i)
done
done 3<$2
#Now, array has all the desired values. I need to print them out.
Here is what is the desired output:
1 2 3 4
5 6 7 8
Here is what is inside my array:
(1 2 3 4 5 6 7 8)
{...}does not create an array.my_arryis just a string. You wantmy_arr=(1 2 3 4 5 6 7 8).