I have a text file with matrices
A: 1, 2, 3, 4
B: 2, 4, 6, 8; 1, 2, 5, 6
C: 8, 6, 4, 2; 1, 2, 3, 4; 1, 3, 5, 7
and I want to read this file in shell script, store these matrices in different arrays and then use these arrays for further computation (addition and subtraction only).
Here is my code:
#! /bin/bash
R=`awk -F'[^0-9]+' '{$1=$1; print;}' testfile.txt`
echo $R;
this gives me an output in this form:
1 2 3 4 2 4 6 8 1 2 5 6 8 6 4 2 1 2 3 4 1 3 5 7
I tried using loops to put these in different arrays but that didn’t work for some reason.
Can anyone help me with this?
(i am new to shell scripting so a little explanation with your code solution would be really helpful. thanks)
One of the Laws of Programming is that parsing input is often more difficult than the entire remainder of the exercise.
To get started, you can try using either
cutor the Internal Field Separator several times on each line.First use
:as the delimiter to separate the array name from the values.Then use
;as the delimiter to separate the rows.Finally, use
,as the delimiter to get the individual values.