0

I have line like as a parameter for my script

 first element,second element,asd,452

and I want to split it into array to get

 first element
 second element
 asd
 452

and be able to access them like array[1]

How can I achieve this?

EDIT:

now I'm trying

while IFS='' read -r line || [[ -n "$line" ]]; do
 echo "Text read from file: $line"
 IFS=, read -ra arr <<< $line
 for x in $arr
  do
    echo "$x"
  done   
done < "$1"

my file http://pastebin.com/b4RYvUvv

ray20
  • 436
  • 1
  • 4
  • 13

1 Answers1

0

Using read -a to read data into array with a custom IFS:

str='first element,second element,asd,452'
IFS=, read -ra arr <<< "$str"

Show content of array arr:

declare -p arr
declare -a arr='([0]="first element" [1]="second element" [2]="asd" [3]="452")'
anubhava
  • 761,203
  • 64
  • 569
  • 643