I am using a Bash script to use awk to look for this: (0010,0080) The awk will generally look for any numeric value seperated by a comma in between parenthesis. I have done everything I can think of and still can't get ANY results when I append it to a text file). My regex is:

awk '/\([0-9]{4},[0-9]{4}\)/' dcmResults.txt >> ~/export/"$1"/tagResults.txt

I have done almost every variation of using an escape character for the parenthesis including not using any and I get NO results when appending to a text file. Can someone please tell me what I am doing wrong?

UPDATE, here is what the whole script looks like:

echo "Enter SUID: "
read uid
echo "Enter Tag Number: "
read dicomTag

dicomDump() {
        cd ~/export/"$1"
        dcmdump *.dcm > ~/export/"$1"/dcmResults.txt
tagFinder() {
        for i in $(cat ~/export/"$1"/dcmResults.txt); do 
            grep "$2" | awk '/[0-9]{4},[0-9]{4}.*/' dcmResults.txt >> ~/export/"$1"/tagResults.txt
dicomDump "$uid"
tagFinder "$uid" "$dicomTag"
  • 2
    Works here (GNU Awk 4.1.1). – Hauke Laging Dec 12 '14 at 21:13
  • Can't see why it is not working for me. Might be the version of awk I am using.... – ryekayo Dec 12 '14 at 21:17
  • I got GNU Awk 3.1.7 – ryekayo Dec 12 '14 at 21:19
  • Let me post the whole script. – ryekayo Dec 12 '14 at 21:24
  • Not particularly, no – ryekayo Dec 12 '14 at 21:31

Gnu Awk only started including interval expressions (your {4} qualifying the [0-9]) in 4.0:

Interval expressions were not traditionally available in awk. They were added as part of the POSIX standard to make awk and egrep consistent with each other.

Initially, because old programs may use ‘{’ and ‘}’ in regexp constants, gawk did not match interval expressions in regexps.

However, beginning with version 4.0, gawk does match interval expressions by default. This is because compatibility with POSIX has become more important to most gawk users than compatibility with old programs.

For programs that use ‘{’ and ‘}’ in regexp constants, it is good practice to always escape them with a backslash. Then the regexp constants are valid and work the way you want them to, using any version of awk.17

See the manual entry.

  • Wow!!! Really?! lol – ryekayo Dec 12 '14 at 21:28
  • @ryekayo, for (some) lower versions of awk, you may be able to pass in --re-interval to enable support for { and `}' – iruvar Dec 12 '14 at 21:46
  • I actually saw that in it's man page when I looked at it. Never occurred to me the version was why it wasn't working – ryekayo Dec 12 '14 at 21:47

This line strikes me as having two problems:

grep "$2" | awk '/[0-9]{4},[0-9]{4}.*/' dcmResults.txt >> ~/export/"$1"/tagResults.txt
  1. You can do the whole thing in grep:

    grep -e "$2" -e '([0-9]\{4\},[0-9]\{4\})' dcmResults.txt >> ~/export/"$1"/tagResults.txt
  2. Where is grep getting its input, and where is the output of grep going to? Since awk has a filename for input, it would ignore standard input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.