Sponsored Content
Full Discussion: Using foreach with two lists
Top Forums Shell Programming and Scripting Using foreach with two lists Post 302528977 by SimonWhite on Wednesday 8th of June 2011 10:06:44 AM
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

 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
foreach(n)						       Tcl Built-In Commands							foreach(n)

__________________________________________________________________________________________________________________________________________________

NAME
foreach - Iterate over all elements in one or more lists SYNOPSIS
foreach varname list body foreach varlist1 list1 ?varlist2 list2 ...? body _________________________________________________________________ DESCRIPTION
The foreach command implements a loop where the loop variable(s) take on values from one or more lists. In the simplest case there is one loop variable, varname, and one list, list, that is a list of values to assign to varname. The body argument is a Tcl script. For each element of list (in order from first to last), foreach assigns the contents of the element to varname as if the lindex command had been used to extract the element, then calls the Tcl interpreter to execute body. In the general case there can be more than one value list (e.g., list1 and list2), and each value list can be associated with a list of loop variables (e.g., varlist1 and varlist2). During each iteration of the loop the variables of each varlist are assigned consecutive values from the corresponding list. Values in each list are used in order from first to last, and each value is used exactly once. The total number of loop iterations is large enough to use up all the values from all the value lists. If a value list does not contain enough elements for each of its loop variables in each iteration, empty values are used for the missing elements. The break and continue statements may be invoked inside body, with the same effect as in the for command. Foreach returns an empty string. EXAMPLES
This loop prints every value in a list together with the square and cube of the value: set values {1 3 5 7 2 4 6 8} ;# Odd numbers first, for fun! puts "Value Square Cube" ;# Neat-looking header foreach x $values { ;# Now loop and print... puts " $x [expr {$x**2}] [expr {$x**3}]" } The following loop uses i and j as loop variables to iterate over pairs of elements of a single list. set x {} foreach {i j} {a b c d e f} { lappend x $j $i } # The value of x is "b a d c f e" # There are 3 iterations of the loop. The next loop uses i and j to iterate over two lists in parallel. set x {} foreach i {a b c} j {d e f g} { lappend x $i $j } # The value of x is "a d b e c f {} g" # There are 4 iterations of the loop. The two forms are combined in the following example. set x {} foreach i {a b c} {j k} {d e f g} { lappend x $i $j $k } # The value of x is "a d e b f g c {} {}" # There are 3 iterations of the loop. SEE ALSO
for(n), while(n), break(n), continue(n) KEYWORDS
foreach, iteration, list, looping Tcl foreach(n)
All times are GMT -4. The time now is 05:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy