1

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.

Sven
  • 98,649
  • 14
  • 180
  • 226
Shailesh Sutar
  • 1,517
  • 5
  • 23
  • 41

2 Answers2

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

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

Sven
  • 98,649
  • 14
  • 180
  • 226
4

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.