I want to create a 2D array. For example free -h gives me a table. I want to create the same table with 2D array without awk. How can I declare it with declare -a matrix . Thanks Community
-
Welcome to Stack Overflow! Could you please elaborate us your effort showing the necessary part of the code?Enamul Hassan– Enamul Hassan2016-02-11 09:42:45 +00:00Commented Feb 11, 2016 at 9:42
-
i think i have to use head,tail or sed commandsArda Nalbant– Arda Nalbant2016-02-11 09:43:00 +00:00Commented Feb 11, 2016 at 9:43
-
Why are you willing to use head, tail, or sed, but unwilling to use awk? That makes little sense.William Pursell– William Pursell2016-02-11 19:25:33 +00:00Commented Feb 11, 2016 at 19:25
Add a comment
|
1 Answer
It's not supported out of the box, but you can simulate it as explained it below, the catch is when you are looping through you need to pass every line to another array
Example data has file names and columns for these
browser browserId browserName
country countryId countryName
event eventId eventName
Your array declaration will be
myArray=(
"browser browserId browserName"
"country countryId countryName"
"event eventId eventName")
If you like to create a loop to process each line
for afile in "${myArray[@]}"
do
aline=($afile)
filename=${aline[0]}
column1=${aline[1]}
column2=${aline[2]}
done
the line
afile=($afile)
passes each line from myArray to another array called aline.
I have used this and works. Feel free to ask if you have any more questions