Shell script :: Reading a column from file & store it in array

20,538

Solution 1

You can also do:

  name=( $(awk '{print $3}' ./info.txt) )

I find this a little simpler. You can then access the array like ${name[1]} ${name[2]} or use * instead of a number for all elements.

Solution 2

name=( $(cut -d ' ' -f 3 "./info.txt") )

will do what you want (starting with index 0 however).

Share:
20,538

Related videos on Youtube

Shailesh Sutar
Author by

Shailesh Sutar

All posts are imaginary and fictional. A resemblance to any current or past affair is pure coincidence. IT Engineer by profession. Curious about human behavior.

Updated on September 18, 2022

Comments

  • Shailesh Sutar
    Shailesh Sutar over 1 year

    I am working on shell script where i want to store O/P of the command in array. I have a file containing rows & columns from where i want to extract 3rd column & store all values in a array. if suppose i have below in my file info.txt

      abc  xyz  pqr  akl
      mnt  var  man  lak
      qer  tag  sam  bob
    

    I want to store pqr, man and sam in array lets call name[1], name[2], name [3]

    Can someone please help me with this.