Concatenate Loop Results


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenate Loop Results
# 1  
Old 03-10-2010
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 07:38 AM.. Reason: Please use code tags!
# 2  
Old 03-10-2010
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
Quote:
Originally Posted by Franklin52
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
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 07:55 AM..
# 5  
Old 03-10-2010
Quote:
Originally Posted by malcomex999
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 Smilie

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
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
Quote:
Originally Posted by radoulov
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Df -h results in a loop

Hello everyone, I am doing a check of the disk space using df -h, I want to combine the result in break line; but the result after while/done is empty: # df -h Filesystem Size Used Avail Use% Mounted on rootfs 20G 14G 4.6G 75% / /dev/root 20G 14G 4.6G 75% /... (15 Replies)
Discussion started by: Abu Rayane
15 Replies

2. Shell Programming and Scripting

How to concatenate grep results?

hi, let's say we have input in files test1.txt, test2.txt, text3.txt ... ... ... ('...' means more files & lines not just 'dots') test1.txt has: A B C D ... ... ... test2.txt has A B C D ... ... ... (4 Replies)
Discussion started by: msonoth
4 Replies

3. UNIX for Dummies Questions & Answers

Concatenate strings in a a for loop

hi guys, I have this question. I am creating an script to that read a text file(.ini) with the list of the patterns to find for example: EPMS_VO EMPS_PARTS Then it check if file have been delivered in a folder and process it with each pattern, but I am having problems concatenting the... (7 Replies)
Discussion started by: Danman
7 Replies

4. Shell Programming and Scripting

Narrowing sed Results in While Loop

find $SRC -type f -name *.emlx | while read FILE do if : then sed -n '/From/p' $FILE fi done > $DEST-output.txt The loop above spits out a .txt file with several lines that look like this: From: John Smith <jsmith@company.com> How can I narrow that sed result to spit out the email... (5 Replies)
Discussion started by: sudo
5 Replies

5. Shell Programming and Scripting

Loop through awk results

I have files structured in stanzas, whose title is '', and the rest couples of 'id: value'. I need to find text within the title and return the whole stanzas that match the title. The following works: awk 'BEGIN{RS="";IGNORECASE=1}/^\/' myfileI would need to count all of the occurences, though,... (7 Replies)
Discussion started by: hermes14
7 Replies

6. Shell Programming and Scripting

S# in a for loop - concatenate $(loop counter)

Hi, hope I am posting in the right section. My problem is that I have 2 or more arguments passed and I want to check if the arguments passed exists or not. The first argument should not exist and the remaining others should exist. example: ./shells.sh argument1 argument2 argument3 ... (5 Replies)
Discussion started by: fight4love
5 Replies

7. Shell Programming and Scripting

Not able to store the results of perl recursive function when applied under for loop

Hi Perl Gurus , need URGENT HELP PLEASE !!!!! I have one recursive Perl function which takes path of any directory as argument and returns array containing all the sub folders inside it recursively. Now the problem is that it works well if i use it with one time but the problem is that when... (0 Replies)
Discussion started by: anthriksh2000
0 Replies

8. Shell Programming and Scripting

doing a for loop adding up the results

Hi there If I run a 'swap -l' on my solaris box, i get swapfile dev swaplo blocks free /dev/dsk/c1t0d0s1 54,65 8 67119560 65655144 /dev/dsk/c1t0d0s2 54,65 8 33119522 32655122 I wanted to run a for loop adding up the totals of each column 4 , excluding the... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

9. Shell Programming and Scripting

2 CMD results on the same line while rexing in a loop

Folks, I have a 3 problems. In a sh script, I call a server name from a list and rex to a distant machine to get the boot date. for i in `cat list` do (echo "$i|"; /bin/rexsh $i -l bozo -t10 who -b | cut -d" " -f14-16) >>getBootTimes.out sleep 1 done The results are on 2 lines instead... (8 Replies)
Discussion started by: linux_lou
8 Replies

10. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies
Login or Register to Ask a Question