Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Foreach not looping through all items Post 303031049 by IlaBert on Wednesday 20th of February 2019 11:35:19 PM
Old 02-21-2019
Foreach not looping through all items

Hello, very new to this and have to use tcsh (not a choice at the moment).

I need to convert files within sub directories. My foreach loops don't work.
Directories are as follow:
Code:
sub-001
--ses-T1
---- anat
--------File.nii
--ses-T2
----anat
 -------File.nii
sub-002

and so on.

My script works only for the first sub-001 and does not move on to the second sub-002. It perfectly executes the operation on my .nii file. (3dcopy)
What am I doing wrong with the foreach that should go through all sub-*?

Code:
set data_root = /Users/whatever/ever/Scans
# --------------------------------------------------
# get a list of subjects
  cd $data_root
  set subjects = ( sub-* )
  echo $subjects

# --------------------------------------------------
# process all subjects
  foreach subj_id ( $subjects )
     
     echo $subj_id
     # foreach subj_id get list of sessions
     cd $data_root/$subj_id
     set session = ( ses-T* )
     echo $session
  
     # for all sessions 
     foreach session_num ( $session )
		  
		  #move to anat directory
		  cd $data_root/$subj_id/$session_num/anat
		  #for all items
		  foreach item ( * )
		  #create variable filename with part of file before the "." 
		  set filename = (`echo $item | awk -F '[.]' '{print $1}'`)
		  #convert nii to AFNI file and run 3dcopy 
		  3dcopy $item $filename 
                 #make a new directory called nii
                  mkdir  $data_root/$subj_id/$session_num/anat/nii
                 #move all nii.gz files to new directory
                 mv *.nii.gz $data_root/$subj_id/$session_num/anat/nii    
     end
     
  end

Moderator's Comments:
Mod Comment MOD's comment: Please do wrap your samples, codes in [CODE]........[/CODE] tags as per forum rules.

Last edited by RavinderSingh13; 02-21-2019 at 12:41 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing items in 2 files

Hi, I have 2 files with contents in them and I need to compare each item in each file. File1: item4 item5 File2: item2 item3 item5 item6 The items names can be of different lengths. If the items in the File1 are not in File2, delete the missing item in File1. The resulting... (12 Replies)
Discussion started by: ReV
12 Replies

2. Gentoo

Linux FAQ Items

Hello Guys, Sometimes it is necessary to add more swap space after installation. For example, you may upgrade the amount of RAM in your system from 64 MB to 128 MB, but there is only 128 MB of swap space. It might be advantageous to increase the amount of swap space to 256 MB if you perform... (11 Replies)
Discussion started by: prashant_ohol
11 Replies

3. Shell Programming and Scripting

awk between items including items

OS=HP-UX ksh The following works, except I want to include the <start> and <end> in the output. awk -F '<start>' 'BEGIN{RS="<end>"; OFS="\n"; ORS=""} {print $2} somefile.log' The following work in bash but not in ksh sed -n '/^<start>/,/^<end>/{/LABEL$/!p}' somefile.log (4 Replies)
Discussion started by: Ikon
4 Replies

4. UNIX for Dummies Questions & Answers

Searching multiple items

Hi, I'm a complete newbie so bear with me. I have a directory (and sub-dirs) full of .doc, .xls files. What I'm trying to do is do a single search within the files (i.e. within each .doc etc) for occurrences of multiple items e.g. apples, pears, grapes, bananas. Basically I'd provide a... (4 Replies)
Discussion started by: kainfs
4 Replies

5. Shell Programming and Scripting

Help needed regarding first 3 items in the list

Hi, I've a list in the following format: Empdept filedetails buildingNo Area AAA 444 2 juy AAA 544 2 kui AAA 567 4 poi AAA 734 5 oiu AAA 444 ... (2 Replies)
Discussion started by: skpvalvekar
2 Replies

6. UNIX for Dummies Questions & Answers

queue items...

Hi everyone. I have a lot of programs i want to run on some data files, they need to be done sequentially. Often the output from one program is the input for the next. e.g $ progA data1 > data1.A $ progB data1.A > data1.AB $ progC data1.AB > data1.ABC repeat on data2, 3, 4, 5, 6 etc ... (4 Replies)
Discussion started by: Jay-pea
4 Replies

7. Shell Programming and Scripting

Matching 2 items in a string

Little lost here, I am trying to search a line for both values after the $ signs. My ultimate goal is to get percertage. <?php $string = "Something on sale for $4 and orginal price $10"; $strstr =. strstr($string, '$'); $strrchr =. strrchr($string, '$'); echo "$strstr<br>"; echo... (1 Reply)
Discussion started by: mrlayance
1 Replies

8. Programming

Removing Items In A ListView

Hi everyone! So I have a listView on my Form named "officeView" I already have the code to add and update info into it, but Im having troubles deleting items out of it. :/ Now I know how to delete an Item from the listView, but I want the item before the deleted item to become automatically... (0 Replies)
Discussion started by: romeo5577
0 Replies

9. Shell Programming and Scripting

Moving items to a new line

Hi, Let's I have the following strings (md5): 07177edf8261d28c6a003e583fcbe38c 0717c0037b3a20fc0f0998e673f228d5 0717d611a5d24374628b98e17fd00977,0717d611a5d24374628b98e17fd00977 07189a18afdae558bb5aadfe602e4a91 0719e97d481c239667f38a3e166bed74 071af3225fe50a1fdbb42c43aac313cc... (4 Replies)
Discussion started by: talfiq
4 Replies

10. Shell Programming and Scripting

Parsing through a list of items

Hi there, Here is my checklist of items, 4.1.1 Alerter 4.1.2 Client Services for Netware 4.1.3 Clipbook 4.1.4 Fax Service 4.1.5 File Replication 4.1.6 File Services for Macintosh 4.1.7 FTP Publishing Service 4.1.8 Help and Support 4.1.9 HTTP SSL 4.1.10 IIS Admin Service ... (1 Reply)
Discussion started by: alvinoo
1 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 03:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy