Writing output to a file in columns

Hi

I am trying to write output to a file in columns
I have file in the follwoing:

# cat file
abc
def
#

I am trying to write next output as like

# cat file
abc 123
def 345
#

:mad:

Hello Priya Amaresh,

Could you please explain by which logic you want output like as follows.

abc 123
def 345

It is advisable to always provide complete information about your requirement with your os details and what you have tried so far,
it will help us too to understand your requirement.

Thanks,
R. Singh

HI Ravinder

I am trying to write into a file in the form of array.
Whereas the column attribute remains constant and row entries will be held.
I tried the following

declare -A matrix
num_columns=3
num_rows=2
for ((i=1;i<=num_rows;i++)) do
echo "Enter the class of student $i:"
read name
matrix[$i,1]=$oss >> /home/info
done
cat /home/info

here matrix[$i,1] ==> means row value varies but column remains one.. but I missing a link between column variable "num_columns" and "matrix[$i,1]"

Could you please tel me how to enter variables into matrix keeping column value constant for a while and varying row values

Is that homework?

AFAIK: Bash doesnt allow double indexed arrays (matrix[$i,1] - or whatever the proper term is) -- that much regarding to 'here matrix[$i,1] means'...

Also, while a list of students of a class can be easy to handle, to manage students of classes, i'd recomend to go csv (tablesheet) or mysql (database)...
In both cases the script will have increased difficulty.

Other than that, awaiting answer to RudiC's question.
And still missing the 'logic' behind it, i cant follow the current description - to my understanding, you're just stating the obvious, but not the reason behind it.

@sea

You can use multi-dimensional arrays in bash, but it is a bit of a messy eval hack job

1 Like

Actually, with an associative array, this looks like a multidimensional array:

echo ${!matrix[@]}
2,1 1,1
1 Like