Using foreach with two lists


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using foreach with two lists
# 1  
Old 06-08-2011
Using foreach with two lists

Hi everybody,

I'm trying to use a foreach command with two lists. The file.txt looks like this:
Code:
var1: 100 200 300
var2: 3 6 9

I'm trying to use a foreach command to associate the two variables together. My script looks like this:
Code:
#! /bin/tcsh 

set a=(`cat file.txt | grep 'var1' | cut -d ':' -f2`)
set b=(`cat file.txt | grep 'var2' | cut -d ':' -f2`)

foreach f (${a}) 
foreach b (${b})

echo "var1 = " $a
echo "var2 = " $b

end
end

My output at the moment is:
Code:
var1 =  100
var2 =  3
var1 =  100
var2 =  6
var1 =  100
var2 =  9
var1 =  200
var2 =  6
var1 =  300
var2 =  9

What I want the output to be is:
Code:
var1 =  100
var2 =  3
var1 =  200
var2 =  6
var1 =  300
var2 =  9

Does anybody know how I can do this? I think it should be pretty simple but I'm useless at this!

Thanks in advance

Last edited by Franklin52; 06-08-2011 at 06:11 AM.. Reason: Please use code tags
# 2  
Old 06-08-2011
Code:
kent$  echo "var1: 100 200 300
var2: 3 6 9"|awk '{gsub(/:/,"",$1);for(i=2;i<=NF;i++)if(NR==1)a[i]=$1" = "$i;else{print a[i];print $1" = "$i;}}'
var1 = 100
var2 = 3
var1 = 200
var2 = 6
var1 = 300
var2 = 9

This User Gave Thanks to sk1418 For This Post:
# 3  
Old 06-08-2011
foreach command

Hi,

Thanks for your reply. The code does what I require but I will be changing file.txt with different values for var1 and var2. Also, instead of

Code:
echo "var1 = " $a
echo "var2 = " $b

I will be running processes which require the variables given. This is why I was using the foreach command. I don't think I can use the code that you supplied for this purpose. Sorry if this doesn't make sense. Let me know if I need to explain it clearer,

Simon

Last edited by rbatte1; 09-21-2016 at 07:52 AM..
# 4  
Old 06-08-2011
could you paste a 'file.txt' as example, and explain what are you expecting for exactly?

---------- Post updated at 15:50 ---------- Previous update was at 15:38 ----------

oh, i see what you mean. try this : (not tested, I don't have foreach here):
Code:
#! /bin/tcsh   
set a=(`cat file.txt | grep 'var1' | cut -d ':' -f2`) 
set b=(`cat file.txt | grep 'var2' | cut -d ':' -f2`) 
 foreach f (${a}) b (${b})  
echo "var1 = " $a
echo "var2 = " $b 
 end

# 5  
Old 06-08-2011
Thanks for the reply. I'll copy and paste my code. I tried to make it simpler for the initial post but I think this might make it clearer:

file.txt

Code:
Group_ID: A1991
OBS_ID: 914 3188 3189
BIN_ID: 2000 100 500

shell script:

Code:
#! /bin/tcsh 
# IMAGE PROCESSING SCRIPTS

set g=(`cat file.txt | grep 'Group_ID' | cut -d ':' -f2`)
echo $g
set obs_id=(`cat file.txt | grep 'OBS_ID' | cut -d ':' -f2`)
echo $obs_id
set b=(`cat file.txt | grep 'BIN_ID' | cut -d ':' -f2`)
echo $b

foreach f (${obs_id})
foreach b (${b})
echo $f
echo $b

cd ${f}

bin_radial.csh ${g}/${g}_${f}.set ${b}
prep_spec_files.csh ${g}/${g}_${f}.set ${g}/prof/${b}

end

So I was hoping that it would run through the two .csh scripts with the combinations of (914, 2000) then (3188, 100) and finally (3189, 500). The script above does it in the following order, (914, 2000), (914, 100), (914, 500), (3188, 500) and finally (3189, 500).

Thanks for your help.

Last edited by SimonWhite; 06-08-2011 at 11:04 AM.. Reason: Added code tags
# 6  
Old 06-08-2011
does it work if you write two 'foreach' into one line? i.e
Code:
foreach f (${a}) b (${b})   
echo "var1 = " $f 
echo "var2 = " $b 
  end

# 7  
Old 06-08-2011
New method

Quote:
Originally Posted by sk1418
could you paste a 'file.txt' as example, and explain what are you expecting for exactly?

---------- Post updated at 15:50 ---------- Previous update was at 15:38 ----------

oh, i see what you mean. try this : (not tested, I don't have foreach here):
Code:
#! /bin/tcsh   
set a=(`cat file.txt | grep 'var1' | cut -d ':' -f2`) 
set b=(`cat file.txt | grep 'var2' | cut -d ':' -f2`) 
 foreach f (${a}) b (${b})  
echo "var1 = " $a
echo "var2 = " $b 
 end

Thanks for the effort. I've tried your code but the output is now this:

Code:
ObsID =  914
Bin size =  2000 100 500
ObsID =  3188
Bin size =  2000 100 500
ObsID =  3189
Bin size =  2000 100 500
ObsID =  )
Bin size =  2000 100 500
ObsID =  b
Bin size =  2000 100 500
ObsID =  (
Bin size =  2000 100 500
ObsID =  2000
Bin size =  2000 100 500
ObsID =  100
Bin size =  2000 100 500
ObsID =  500
Bin size =  2000 100 500

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Foreach issue

Hello, I found that this foreach should work with two lists (source: Wikipedia.org) foreach i {1 2 3} j {a b c} { puts "$i $j"} == I try smth. like: With two text files: first.part second.part foreach first (`cat first.part`) second (`cat second.part`) toolcommand $first... (22 Replies)
Discussion started by: unknown7
22 Replies

2. Shell Programming and Scripting

Learning foreach

im newbie at shell scripting. why do the following code #!/bin/tcsh setenv CBC ~/cbc/models/ foreach mix (p00p00 p02p00 p02p04) echo $mix cp $CBC/*$mix*Gyr*fits $mix/ end print(copy) only the first mix? % ./copyfromcbc.sh p00p00 wasn't it supposed to run through all words... (0 Replies)
Discussion started by: prtc
0 Replies

3. Shell Programming and Scripting

Shell Script to Create non-duplicate lists from two lists

File_A contains Strings: a b c d File_B contains Strings: a c z Need to have script written in either sh or ksh. Derive resultant files (File_New_A and File_New_B) from lists File_A and File_B where string elements in File_New_A and File_New_B are listed below. Resultant... (7 Replies)
Discussion started by: mlv_99
7 Replies

4. UNIX for Dummies Questions & Answers

foreach question

OK, so I am extremely rusty and am just getting back to Unix after 9 years. I'm stuck on something easy. I want to search line-by-line for a string in a file, and I want to do this to a series of files in a directory. This works fine to do the search: while read i; do grep $i file2; done... (3 Replies)
Discussion started by: moldoverb
3 Replies

5. Shell Programming and Scripting

foreach loop

Hi everyone Does anyone know what is wrong with this script. i keep getting errors foreach filename (`cat testing1`) set string=$filename set depth=`echo "$string" echo $depth end the error is the following testing: line 1: syntax error near unexpected token `(' testing: line 1:... (3 Replies)
Discussion started by: ROOZ
3 Replies

6. Shell Programming and Scripting

foreach loop

Hi Guys, I have a loop which uses a wildcard i.e. foreach f (*) but when I execute the tcsh file in unix then it gives me an error ->>>>>>>foreach: words not parenthesized<<<<<<<<<<- Any help. (1 Reply)
Discussion started by: abch624
1 Replies

7. Shell Programming and Scripting

Foreach loop

What am I doing wrong with this foreach loop? foreach var ($argv) @sum = $sum + $var (4 Replies)
Discussion started by: haze21
4 Replies

8. Shell Programming and Scripting

foreach/grep help!

#!/bin/bash foreach x (67402996 67402998) { grep -a x FINAL2006.dat >> MISSING_RECORDS.dat } I'm trying to pass a list to the variable x, and then grep for that string in FINAL2006.dat... Final2006.dat is in the same folder as my .sh file. I call this with a .cmd file... At any rate,... (6 Replies)
Discussion started by: JimWork
6 Replies

9. UNIX for Dummies Questions & Answers

foreach command ?!

SaLAam What is the best way to change a word withing a files name. I know I'm not clear enough I will give example : - I have in /test/test N number of files like this 1662_WAITING 1666_WAITING 1670_DONE 1678_DONE 1663_WAITING 1667_WAITING 1673_WAITING ... (5 Replies)
Discussion started by: geoquest
5 Replies
Login or Register to Ask a Question