Home > OS >  Bash: Get substring of docker image with version regex from logs
Bash: Get substring of docker image with version regex from logs

Time:08-17

I am using a bash script to parse docker logs and need to extract the docker image with tag. My log outut is something like this -

TestingmkJHSBD,MFV FROM image/something/docker:2.6.1.566-978.7 testing 

How can I extract this string using bash - image/something/docker:2.6.1.566-978.7

None of the regex I used seem to work.

CodePudding user response:

Would you please try the following:

awk '
    /FROM image/ {
        if (match($0, /image[^[:space:]] /))
            print(substr($0, RSTART, RLENGTH))
    }
' logfile
  • The regex image[^[:space:]] matches a substring which starts with image and followed by non-space character(s).
  • Then the awk variables RSTART and RLENGTH are assigned to the position and the length of the matched substring.

CodePudding user response:

Another awk option:

awk '{if ($0 ~ /FROM image/) {for (i=1; i<=NF; i  ) if ($i ~ /^image/) {print $i} }}' <<<"TestingmkJHSBD,MFV FROM image/something/docker:2.6.1.566-978.7 testing"

Output:

image/something/docker:2.6.1.566-978.7

Or using different log string:

awk '{if ($0 ~ /FROM image/) {for (i=1; i<=NF; i  ) if ($i ~ /^image/) {print $i} }}' <<<"bdkjf asfjkklsdfsg FROM image/something/docker:2.6.1.566-978.7 testing"

Output:

image/something/docker:2.6.1.566-978.7
  • Related