Using foreach with two lists

Hi everybody,

I'm trying to use a foreach command with two lists. The file.txt looks like this:

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:

#! /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:

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:

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

kent$  echo "var1: 100 200 300
var2: 3 6 9"|awk '{gsub(/:/,"",$1);for(i=2;i<=NF;i++)if(NR==1)a=$1" = "$i;else{print a;print $1" = "$i;}}'
var1 = 100
var2 = 3
var1 = 200
var2 = 6
var1 = 300
var2 = 9
1 Like

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

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

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):

#! /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 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

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

shell script:

#! /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.

does it work if you write two 'foreach' into one line? i.e

foreach f (${a}) b (${b})   
echo "var1 = " $f 
echo "var2 = " $b 
  end

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

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

foreach - Linux Command - Unix Command

take a look the 2nd example. I think it should be what you want.

Is the second example:

If so, the output is still:

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

Maybe I will need to use a while loop?

---------- Post updated at 02:25 PM ---------- Previous update was at 11:29 AM ----------

Hi,

Thanks for your replies. I've managed to solve the problem by doing the following:

while ($i<4)

foreach f ($obs_id[$i])
foreach b ($bin[$i])

echo "ObsID = " $f
echo "Bin size " = $b

@ i = $i + 1

end
end 
end