2

Here is the code i use to select a string from database. Split and then write to a text file .

#Get information from DB for the given entrynum
RETVAL=`sqlplus -s username/pwd@db <<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
SELECT  (entrynum|| '-'||path||'-'|| syear||'-'|| eyear||'-'|| type) as RET
    FROM  entrydetails WHERE entrynum = 123;

EXIT;
EOF`    

print "$scriptname return value :$RETVAL " 1>&2
#Output :123-/userx/data/tt-2015-2015-1

#split each value into an array
arr=$(echo $RETVAL | tr "-" "\n")

#write to text variable
writeText="export entrynum=${arr[0]}\n
          export path=${arr[1]}\n
          export Syear=${arr[2]}\n
          export eyear=${arr[3]}\n
          export type=${arr[4]}";

#write text to file
echo $writeText > ../in/log_file

The output i get in the file is

export entrynum=123 /userx/data/1 2015 2015 1
 export path=
 export syesr=
 export eyear=
 export type=

Expected result is

export entrynum=123   
export path=/userx/data/1
export syesr=2015
export eyear=2015 
export type=1

1 Answer 1

2

You need one more pair of parens around the array assignment

arr=($(echo $RETVAL | tr "-" "\n"))

the $() is an eval. arr=() is a compound assignment. Split is on whitespace, so you could just even use a space rather than a newline to tr like this

tr "-" " "

if your ksh doesn't support compound assignment you could use set -A

set -A arr -- $(echo $RETVAL | tr "-" " ")

Sign up to request clarification or add additional context in comments.

3 Comments

it works for me -- ksh version sh (AT&T Research) 93u+ 2012-08-01 - alternative is to use an explicit set - I updated the answer to illustrate this
tried this to check the verion [ "echo "\c" | grep c" ] && echo ksh93 || echo ksh88. It is ksh88. Still i get the same error
yes, you need 93 for the =(). I updated the answer to offer an alternative

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.