Search code examples
arraysbashfor-loopgrepdcmtk

bash for loop for given array only accounts for one file out of two files in array


I have an array of two files from the find command:

[ogino@chead SIEMENS]$ DCM_FILES=($(find . -type f))

[ogino@chead SIEMENS]$ echo ${DCM_FILES[@]}
./107939/107939/DICOM/0020_20071002_104554.150005 ./100013/100013/DICOM/0002_20130723_100423.117500

I have then constructed a for loop in order to dump information in the files (via dcmdump of the dcmtk Toolbox):

[ogino@chead SIEMENS]$ for dcm in "${DCM_FILES[@]}"
do
  sd=$(dcmdump ${dcm} | grep "(0008,1030)"); 
done

However, it seems like it is only reading one DICOM file, as displayed in my output:

[ogino@chead SIEMENS]$ echo ${sd[0]}
(0008,1030) LO [RESEARCH^THOMPSON_SCHILL] # 24, 1 Unknown Tag & Data

[ogino@chead SIEMENS]$ echo ${sd[1]}

# I get a blank line for the second instance of my echo testing

[ogino@chead SIEMENS]$ echo ${sd[@]}
(0008,1030) LO [RESEARCH^THOMPSON_SCHILL] # 24, 1 Unknown Tag & Data

I tried initiating a blank array for sd before the loop, that didn't work (sd=( )), and surprisingly, indexing with dcm also didn't seem to pick up the second file ${sd[1]}...

[ogino@chead SIEMENS]$ echo ${dcm[0]}
./100013/100013/DICOM/0002_20130723_100423.117500
[ogino@chead SIEMENS]$ echo ${dcm[1]}

[ogino@chead SIEMENS]$ echo ${dcm[@]}
./100013/100013/DICOM/0002_20130723_100423.117500

Removing the variable assignment for the sd variable seems to remove the error, except, I need to store this particular output somewhere with an array, which is why I set the dcmdump command to sd in the first place.

[ogino@chead SIEMENS]$ for dcm in "${DCM_FILES[@]}"; do dcmdump ${dcm} | grep "(0008,1030)"; done
W: no data dictionary loaded, check environment variable: DCMDICTPATH
(0008,1030) LO [MRI BRAIN/MRHD]                         #  14, 1 Unknown Tag & Data
W: no data dictionary loaded, check environment variable: DCMDICTPATH
(0008,1030) LO [RESEARCH^THOMPSON_SCHILL]               #  24, 1 Unknown Tag & Data

Googling around seems to hint to me that there may be an issue with grep, but it's not so clear to me at the moment.

Is there a modification I need to make in my loop to make sure I can store both instances into an array? Any help to point me in the right direction to produce the output above with the second instance of grep


Solution

  • How about iterating over the indexes of your DCM_FILES array instead of the values, since you want the indexes to match in your new array anyway?

    DCM_FILES=($(find . -type f))
    for idx in $(seq ${#DCM_FILES[@]}) ; do
      sd[idx]=$(dcmdump ${DCM_FILES[idx]} | grep "(0008,1030)")
    done
    echo "${sd[@]}"