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.