shalua
April 10, 2007, 1:27pm
1
Hi,
I have a comma delimited string, e.g. empno, ename, sal.
Using Korn Shell Script I want to build the SET clause for an UPDATE statement, and set clause should look like this:
empno=decode(:empno, '?', empno, :empno),
ename=decode(:ename, '?', empno, :ename),
sal=decode(:sal, '?', sal, :sal)
This comma seperated string can have any number of attributes in it seperated by commas.
Any suggestions will be appreciated.
Thanks
Shalu
/tmp/string.txt:
empno, ename, sal
#!/usr/bin/ksh
STRINGFILE=/tmp/string.txt
for i in $(sed 's/,//g' "$STRINGFILE")
do
echo "$i=decode(:$i, '?', $i, :$i),"
done
output:
empno=decode(:empno, '?', empno, :empno),
ename=decode(:ename; '?', ename, :ename),
sal=decode(:sal, '?', sal, :sal),
second line differs from your first post, do you really want:
ename=decode(:ename, '?', empno, :ename)
?
and the third line has a komma at the end
if this is really wanted I 'll update the script
shalua
April 10, 2007, 2:13pm
3
Thanks!! This will be a good learning for me. There are three things:
empno was a typo, so what you did is correct.
I don't want comma after the last line.
also if any attribute in the delimited string is in uppercase I would like to convert it into lower case.
#!/usr/bin/ksh
STRINGFILE=/tmp/string.txt
for i in $(sed 's/,//g' "$STRINGFILE")
do
echo "$i=decode(:$i, '?', $i, :$i),"
done | tr [:upper:] [:lower:] | sed '$ s/,$//'
should do the job
shalua
April 10, 2007, 2:34pm
5
Basically this delimited string is the header of a file, and it is tab delimited not comma. So if I tweak your code like following, I believe I am doing something wrong, because I don't get anything in return:
STRINGFILE=head -1 /home/z1uals/src/pdsa/mydat/textDMR.txt | tr '\t' ','
for i in $(sed 's/,//g' "$STRINGFILE")
do
echo "$i=decode(:$i, '?', $i, :$i),"
done | tr [:upper:] [:lower:] | sed '$ s/,$//'
#!/usr/bin/ksh
TEXTFILE=/home/z1uals/src/pdsa/mydat/textDMR.txt
for i in $(head -1 $TEXTFILE | tr '\t' ',' | sed 's/,//g')
do
echo "$i=decode(:$i, '?', $i, :$i),"
done | tr [:upper:] [:lower:] | sed '$ s/,$//'
can be optimized ^^
shalua
April 10, 2007, 2:49pm
7
This returns a value, but it is not in the correct format as we are expecting.
post the output of:
head -1 /home/z1uals/src/pdsa/mydat/textDMR.txt | tr '\t' ','
shalua
April 10, 2007, 2:54pm
9
Dmr_run_id,rma_server_id,instrument_id,portfolio_id,oas,price
yep because it has no spaces after komma as you wrote in your first post
use:
#!/usr/bin/ksh
TEXTFILE=/home/z1uals/src/pdsa/mydat/textDMR.txt
for i in $(head -1 $TEXTFILE | tr '\t' ',' | sed 's/,/ /g')
do
echo "$i=decode(:$i, '?', $i, :$i),"
done | tr [:upper:] [:lower:] | sed '$ s/,$//'
shalua
April 10, 2007, 2:57pm
11
And this is the output of head -1 /home/z1uals/src/pdsa/mydat/textDMR.txt:
DMR_RUN_ID RMA_SERVER_ID INSTRUMENT_ID PORTFOLIO_ID OAS PRICE
shalua
April 10, 2007, 2:57pm
12
Basically the header of the file is tab delimited.
then leave both tr and sed
#!/usr/bin/ksh
TEXTFILE=/home/z1uals/src/pdsa/mydat/textDMR.txt
for i in $(head -1 $TEXTFILE)
do
echo "$i=decode(:$i, '?', $i, :$i),"
done | tr [:upper:] [:lower:] | sed '$ s/,$//'
shalua
April 10, 2007, 4:13pm
14
Thanks much for all help. I am new to shell scripting, could you please point me to a good shell scripting tutorial.
no problem
KSH script BASICS seems to be ok