|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | 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 and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Concatenate Loop Results
Hi, I have the following situation: Code:
Param1Values = AAAA,BBBB
Param1=$(echo $Param1Values| tr "," "\n")
for x in $Param1
do
db2 select X from Y where Z IN ('$x')
doneObviously the above will perform the select 'x' amount of times. Is there a way in which i can concatanate the results of the loop into one string, so i can perform the select statement once? So currently i will have: Code:
db2 select X from Y where Z IN ('AAAA')
db2 select X from Y where Z IN ('BBBB')Is there a way in which manipulating the above will result in: db2 select X from Y where Z IN ('AAAA','BBBB') NOTE: Param1Values will not have a set amount of values i.e. it wont always be 2 parameters, it can be different everytime. Cheers Last edited by radoulov; 03-10-2010 at 06:38 AM.. Reason: Please use code tags! |
| Sponsored Links | ||
|
|
|
#2
|
|||
|
|||
|
Something like: Code:
Param1Values="AAAA,BBBB"
x=$(echo $Param1Values | sed "s/\(.*\),\(.*\)/'\1','\2'/")
db2 select X from Y where Z IN ('$x')Regards |
|
#3
|
|||
|
|||
|
Quote:
Will the above cater for an infinate amount of values that could be stored in the parameter? Cheers |
|
#4
|
|||
|
|||
|
if you have more than two values... Code:
Param1Values="AAAA,BBBB,CCC,DDD"
x=$(echo $Param1Values | awk -F, -v s=\' '{for(i=1;i<=NF;i++) {printf s $i s;printf (i==NF?_:",")}}' )
db2 select X from Y where Z IN ('$x')Last edited by malcomex999; 03-10-2010 at 06:55 AM.. |
|
#5
|
|||
|
|||
|
Quote:
![]() The above gives the result: db2 select X from Y where Z IN (''AAAA''BBBB''CCC''DDD'') How would i get the code to give the results in the format: db2 select X from Y where Z IN ('AAAA','BBBB''CCC','DDD') So that there is single quotes around each string, and comma seperated? Thanks once again! |
|
#6
|
||||
|
||||
|
Code:
perl -e'
printf "select X from Y where Z IN (%s)\n",
join "," , map "\47$_\47", split ",", $ENV{Param1Values}
'You should export the Param1Values variable before executing the script: Code:
export Param1Values |
|
#7
|
|||
|
|||
|
Quote:
Cheers |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Not able to store the results of perl recursive function when applied under for loop | anthriksh2000 | Shell Programming and Scripting | 0 | 02-12-2010 06:58 AM |
| doing a for loop adding up the results | hcclnoodles | Shell Programming and Scripting | 2 | 12-08-2009 09:14 AM |
| 2 CMD results on the same line while rexing in a loop | linux_lou | Shell Programming and Scripting | 8 | 10-09-2009 04:46 PM |
| Soundtrack Pro: Unexpected results when you apply Time Stretch to an Apple Loop | Linux Bot | OS X Support RSS | 0 | 07-23-2009 05:30 PM |
| Perl - Iterating a hash through a foreach loop - unexpected results | quantumechanix | Shell Programming and Scripting | 5 | 12-15-2003 06:08 PM |