K
K
ksvdon2017-04-28 11:57:00
linux
ksvdon, 2017-04-28 11:57:00

How to put the output of a command line by line in a bash array?

I have some string. I want to split it on the delimiter " , ". Everything turns out, but I want to make each element that I broke into an array element. If you write all this into a variable/immediately into an array, you get a continuous string (and after all, separate lines were originally obtained) and the elements of the array will become words separated by spaces, which is not good. The dumb way is to write everything to a file and count it line by line, it works. But I would like to do without files. Is it possible somehow?
a='trololo oo, lalala aa, lalala''
awk '{split($0,stuff,","); for(i in stuff) { print(stuff[" i "]= stuff[i])} }'
ps it is necessary that trololo oo be the first element, not trololo - 1st, and oo - 2nd...

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
ksvdon, 2017-04-28
@ksvdon

a='{"message":"done it","result":"1204","status":200}'
array_num=0
while read LINE; do
curl_answ_array[${array_num}]=${LINE}
let array_num=${array_num}+1
done < <(echo ${a}|grep status -m 1|tr -d '\"{}'|awk ' {split($0,stuff,","); for(i in stuff) { print(stuff[" i "]= stuff[i])} }')

X
xibir, 2017-04-28
@xibir

a='trololo oo, lalala aa, lalala'
mapfile -t -d, strings < <(echo "$a")
echo "${strings[0]}"
echo "${strings[1]}"

mapfile has the -d option in bash since version 4.4

V
Victor Taran, 2017-04-28
@shambler81

I don't quite understand what you need, but maybexargs -l

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question