I have a private container registry behind a firewall that will need some Google registry images. To make sure things are updated sanely, I'm writing a mirror script in Bash. I have one line that lists the images without their registry URIs, but it's overly complicated and I am piping through 3 awk statements.

docker images | awk '{printf("%s\:%s\n", $1, $2)}' | awk 'NR>1' | awk -F '/' '{print $3}'

First awk statement:

$ docker images | awk '{printf("%s\:%s\n", $1, $2)}'

Second awk statement:

$ docker images | awk '{printf("%s\:%s\n", $1, $2)}' | awk 'NR>1'

Third awk statement:

$ docker images | awk '{printf("%s\:%s\n", $1, $2)}' | awk 'NR>1' | awk -F '/' '{print $3}'

How can I reduce this to one awk statement?


It looks like the purpose of the second command is to skip the REPOSITORY:TAG table header, which would always be the first line of the docker images output. You can therefore combine the first two awk commands as:

awk 'NR > 1 { printf("%s:%s\n", $1, $2) }'

To keep just the trailing part of the REPOSITORY field, you could do gsub('.*/', '', $1). Note that that is not the same as awk -F '/' '{print $3}', but likely better.

Therefore, I would write the solution as

docker images | awk 'NR > 1 { gsub('.*/', '', $1); printf("%s:%s\n", $1, $2); }'

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.