![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
/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 Code:
empno=decode(:empno, '?', empno, :empno), ename=decode(:ename; '?', ename, :ename), sal=decode(:sal, '?', sal, :sal), 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. |
|
|||
|
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. |
|
|||
|
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/,$//' |
|
|||
|
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/,$//' |