2

I'm trying to do a query and store every row result in an array element in ksh (maybe bash). I do:

result=($($PATH_UTI/querysh "
set heading off
set feedback off
SELECT columnA,columnb FROM user.comunication;"))

I have that:

row1 = HOUSE CAR
row2 = DOC   CAT
echo "${result[1]}" and it gives me HOUSE

But I would like to get:

echo "${result[1]}" gives: "HOUSE CAR"

2 Answers 2

3

You need to change default separator IFS to split data by end of line character and disable globbing with set -f to avoid issues with strings containing e.g. * or ?:

$ IFS=$'\n'
$ set -f
$ result=( $(printf "HOUSE CAR\nDOC   CAT") )
$ echo "${result[0]}"
HOUSE CAR
$ echo "${result[1]}"
DOC   CAT

Note that both changes will stay in effect for the rest of the script unless changed back.

3

In Bash you can use mapfile (it should be tested with your actual result):

# note that the parenthesis are not needed
$ result="HOUSE CAR
DOC   CAT"
$ mapfile -t arr < <(printf "%s" "$result")
$ echo "${arr[0]}" # or 1 if the first row is empty
HOUSE CAR

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.