Notification texts go here Contact Us Buy Now!

how to loop through a nested loop so that the main loop and the nested one can go in parallel

#! /bin/sh set -o nounset WorkDir='/data/mri_measures/analysis/set_files' paste subjects.txt number_of_slices.txt \ | while read -r ID Measure do File=""${WorkDir}/${ID}.xmlg"" if test -f ""$File"" then sed -i ""s/number_of_brain_slices/${Measure}/g"" "$File" else echo ""missing xmlg file for subject \"$ID\"!"" fi done

With the paste command, you can merge two files line-wise. In this case, the two files are subjects.txt and number_of_slices.txt. The while loop is used to process each line of the merged file. Inside the loop, the if statement checks if the XMLG file for the subject exists. If it does, the sed command is used to replace the string "number_of_brain_slices" with the value of the Measure variable. If the XMLG file does not exist, an error message is printed.

This code can be used to parallelize the processing of the XMLG files by using a tool like GNU Parallel. For example, the following command would process the XMLG files in parallel, using 10 jobs:

paste subjects.txt number_of_slices.txt | parallel -j 10 --colsep ' ' --joblog joblog.txt 'echo {} | while read -r ID Measure; do echo ${ID} ${Measure}; if test -f "/data/mri_measures/analysis/set_files/${ID}.xmlg"; then sed -i "s/number_of_brain_slices/${Measure}/g" "/data/mri_measures/analysis/set_files/${ID}.xmlg"; else echo "missing xmlg file for subject \"$ID\"!"; fi; done'

This command would create a job log file called joblog.txt that contains the output of each job.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.