Skip to content

Instantly share code, notes, and snippets.

@ageldama
Last active March 8, 2025 03:55
Show Gist options
  • Save ageldama/a8fc15bb69dc6d0581a95c33f61945f1 to your computer and use it in GitHub Desktop.
Save ageldama/a8fc15bb69dc6d0581a95c33f61945f1 to your computer and use it in GitHub Desktop.
#!/bin/bash
# DEPENDENCY: Bash
# DEPENDENCY: Perl v5.8+
# DEPENDENCY: `file` command
# DEPENDENCY: dcmtk -- https://dicom.offis.de/en/dcmtk/dcmtk-tools/
# SEE: https://www.dicomlibrary.com/dicom/dicom-tags/
# 현재 디렉토리 이하의 DICOM 파일만 검색하여, 특정 DICOM 태그를 모아서 JSON형태출력.
# 예시: `x.json`-파일에 모은 파일이름과 태그값 출력.
#
# bash./find-and-gather-dcm-tags.bash >> x.json
function STDERR () {
cat - 1>&2
}
for f in $(find .); do
FILE_STR=$(file "$f")
if [[ ! $FILE_STR =~ "DICOM medical imaging data" ]]; then
continue
fi
# DICOM 파일 맞음:
echo "DUMPING: ${f} ..." | STDERR
TAGS=$(dcmdump "${f}")
# DICOM Tag: (0008,0050) SH Accession Number
# __________ : regex capture group.
ACCESSION_NUMBER=$(echo "${TAGS}" | perl -ne'/(0008,0050).+\[(.+)\]/ && print $2')
# --------- : DICOM Tag ID
# OUTPUT
printf '{"filename":"%s", "accession_number":"%s"}\n' "${f}" "${ACCESSION_NUMBER}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment