The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Conditonal clause in expect. akbar Shell Programming and Scripting 1 05-13-2008 04:28 PM
if clause palmer18 UNIX for Dummies Questions & Answers 3 08-08-2007 06:22 AM
why put double square brackets in an if clause? napolayan UNIX for Dummies Questions & Answers 5 11-15-2006 11:18 PM
if clause in AWK END block not working. satnamx Shell Programming and Scripting 1 04-21-2006 03:29 AM
GROUP BY clause functionality in a C Program avdtech High Level Programming 2 03-08-2005 06:40 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-10-2007
Registered User
 

Join Date: Apr 2007
Posts: 18
building a SET clause in shell script

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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-10-2007
Registered User
 

Join Date: Nov 2006
Location: Austria/Vienna
Posts: 204
/tmp/string.txt:

Code:
empno, ename, sal
Code:
#!/usr/bin/ksh
STRINGFILE=/tmp/string.txt

for i in $(sed 's/,//g' "$STRINGFILE")
 do
  echo "$i=decode(:$i, '?', $i, :$i),"
 done
output:

Code:
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

Last edited by funksen; 04-10-2007 at 11:00 AM.
Reply With Quote
  #3 (permalink)  
Old 04-10-2007
Registered User
 

Join Date: Apr 2007
Posts: 18
Thanks!! This will be a good learning for me. There are three things:

1. empno was a typo, so what you did is correct.
2. I don't want comma after the last line.
3. also if any attribute in the delimited string is in uppercase I would like to convert it into lower case.
Reply With Quote
  #4 (permalink)  
Old 04-10-2007
Registered User
 

Join Date: Nov 2006
Location: Austria/Vienna
Posts: 204
Code:
#!/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
Reply With Quote
  #5 (permalink)  
Old 04-10-2007
Registered User
 

Join Date: Apr 2007
Posts: 18
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/,$//'
Reply With Quote
  #6 (permalink)  
Old 04-10-2007
Registered User
 

Join Date: Nov 2006
Location: Austria/Vienna
Posts: 204
Code:
#!/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 ^^
Reply With Quote
  #7 (permalink)  
Old 04-10-2007
Registered User
 

Join Date: Apr 2007
Posts: 18
This returns a value, but it is not in the correct format as we are expecting.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0