building a SET clause in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting building a SET clause in shell script
# 1  
Old 04-10-2007
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
# 2  
Old 04-10-2007
/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 03:00 PM..
# 3  
Old 04-10-2007
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.
# 4  
Old 04-10-2007
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
# 5  
Old 04-10-2007
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/,$//'
# 6  
Old 04-10-2007
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 ^^
# 7  
Old 04-10-2007
This returns a value, but it is not in the correct format as we are expecting.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help needed with shell script to search and replace a set of strings among the set of files

Hi, I am looking for a shell script which serves the below purpose. Please find below the algorithm for the same and any help on this would be highly appreciated. 1)set of strings need to be replaced among set of files(directory may contain different types of files) 2)It should search for... (10 Replies)
Discussion started by: Amulya
10 Replies

2. Shell Programming and Scripting

Generating & executing the SELECT and UPDATE clause dynamically using shell script

Hi All, I need to write one shell script. The requirement is as follows. a) I am having list the employee names in a file stored in /tmp directory location as below /tmp/emp.txt b) and the contents are as below cat emp.txt ravi raj ram arun c) I need to connect to sybase... (1 Reply)
Discussion started by: Gowtham_giri
1 Replies

3. Shell Programming and Scripting

Use a shell variable in where clause

Hi all, I want to use a variable inside my sql query and below is my script: #!/bin/ksh export b="abcd" a=`sqlplus -s abc/def@ghi <<++ set heading off; set feedback off; select xxx from mytable where clmn_nm='$b'; exit; ++` echo $a But the output i get is below: $>... (4 Replies)
Discussion started by: Jayaraman
4 Replies

4. Shell Programming and Scripting

$1 stays set after sourcing shell script.

ok, so I have a shell script that can be called using the first argument ($1) or not. This argument is a word (Tim for example) and not an actual flag (-x for example). If I call the script with an argument and call the same script without one, it believes that I provided an argument. Note here... (2 Replies)
Discussion started by: mrwatkin
2 Replies

5. Shell Programming and Scripting

help with if clause in bash script

hey guys, I am trying to get some statistics from a DHCP server, like counting the number of DHCP Discovers from a specific MAC address. The script should count the number of DHCP Discovers and output it, otherwise if it cannot find any , it should output the MAC address and "0". The first... (10 Replies)
Discussion started by: liviusbr
10 Replies

6. Shell Programming and Scripting

Bash-Shell: If-Clause to check if file is empty

Hello, I want to checkl whether my file has text in it or not. if ; then ... if ; then ... But none of these work Can someone help me? ---------- Post updated at 09:00 AM ---------- Previous update was at 08:55 AM ---------- The code-tags caused an displayerror,... (5 Replies)
Discussion started by: ABE2202
5 Replies

7. Shell Programming and Scripting

How to set PATH using shell script [resolved]

Hi, Can anyone help me on how to set PATH using shell scripting.. Please find the shell script code here.... #!/bin/bash PATH = $PATH:/opt/app/oracle/product/10.2.0/bin export PATH echo $PATH exit When i execute this script i get the following error ./backup.sh: line 2: PATH:... (0 Replies)
Discussion started by: srinivasj
0 Replies

8. Shell Programming and Scripting

How do I set up a shell script using ifconfig?

I am running on AIX 5.3. I have a remote AIX server running on a generator. Many times the generator goes out and I only have a window of 15 mins with the network up and 30 mins the server is powered. I need help creating a script using ifconfig, where it goes out and checks the network every 5... (2 Replies)
Discussion started by: AIX25
2 Replies

9. Shell Programming and Scripting

set password using a shell script

Hi All How can I set password in linux.It is OK if it display password in plain text in script. manually i can set: #passwd Changing password for root Enter new password: Bad password: too weak. Re-enter new password: Password changed. # I want this to be done by script.Please let me... (2 Replies)
Discussion started by: tannu
2 Replies
Login or Register to Ask a Question