Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



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

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-10-2010
Registered User
 

Join Date: Mar 2010
Posts: 5
Thanks: 0
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: 5,591
Thanks: 2
Thanked 68 Times in 65 Posts
Something like:

Code:
Param1Values="AAAA,BBBB"

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

Regards
  #3  
Old 03-10-2010
Registered User
 

Join Date: Mar 2010
Posts: 5
Thanks: 0
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: 431
Thanks: 7
Thanked 10 Times in 10 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..
  #5  
Old 03-10-2010
Registered User
 

Join Date: Mar 2010
Posts: 5
Thanks: 0
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!
  #6  
Old 03-10-2010
radoulov's Avatar
--
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 3,619
Thanks: 15
Thanked 55 Times in 54 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

  #7  
Old 03-10-2010
Registered User
 

Join Date: Mar 2010
Posts: 5
Thanks: 0
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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:46 AM.