0

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

3
  • Welcome to Stack Overflow! Could you please elaborate us your effort showing the necessary part of the code? Commented Feb 11, 2016 at 9:42
  • i think i have to use head,tail or sed commands Commented 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. Commented Feb 11, 2016 at 19:25

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.