Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-10-2010
Registered User
 

Join Date: Mar 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
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')
done

Obviously 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  
Old 03-10-2010
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 7,287
Thanks: 55
Thanked 427 Times in 408 Posts
Something like:

Code:
Param1Values="AAAA,BBBB"

x=$(echo $Param1Values | sed "s/\(.*\),\(.*\)/'\1','\2'/")
db2 select X from Y where Z IN ('$x')

Regards
Sponsored Links
    #3  
Old 03-10-2010
Registered User
 

Join Date: Mar 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Franklin52 View Post
Something like:

Code:
Param1Values="AAAA,BBBB"
 
x=$(echo $Param1Values | sed "s/\(.*\),\(.*\)/'\1','\2'/")
db2 select X from Y where Z IN ('$x')

Regards
Thanks for the reply.

Will the above cater for an infinate amount of values that could be stored in the parameter?

Cheers
    #4  
Old 03-10-2010
Registered User
 

Join Date: Sep 2008
Location: In the beautiful World...
Posts: 509
Thanks: 9
Thanked 27 Times in 27 Posts
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..
Sponsored Links
    #5  
Old 03-10-2010
Registered User
 

Join Date: Mar 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by malcomex999 View Post
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","}' | sed 's/,$//') 
db2 select X from Y where Z IN ('$x')

Nealry there

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!
Sponsored Links
    #6  
Old 03-10-2010
radoulov's Avatar
--
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 5,111
Thanks: 92
Thanked 422 Times in 397 Posts

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

Sponsored Links
    #7  
Old 03-10-2010
Registered User
 

Join Date: Mar 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by radoulov View Post
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

Thanks for the reply, much appreciated, but i am trying to stay away from perl and use AWK or SED if possible.

Cheers
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Perl - Iterating a hash through a foreach loop - unexpected results quantumechanix Shell Programming and Scripting 5 12-15-2003 06:08 PM



All times are GMT -4. The time now is 12:55 AM.