Issue
I have a task to set up dockerfile parsing. It is necessary to check whether the image is proxied from dockerfile and if not, then add the address of my repository my.repo.org to the image. for example, my dockerfile with multiple images:
FROM my.repo.org/3.8.7-eclipse-temurin-19
...
###############################################
FROM alpine:3.14
...
################################################
I need to check the name of the image, and if my repository my.repo.org does not exist, then add it to the desired image, and if there is, then do not change anything. What would become:
FROM my.repo.org/3.8.7-eclipse-temurin-19
...
###############################################
FROM my.repo.org/alpine:3.14
...
################################################
I tried using sed and awk, but nothing worked.
Solution
I would harness GNU sed
following way, let file.txt
content be
FROM my.repo.org/3.8.7-eclipse-temurin-19
...
###############################################
FROM alpine:3.14
...
################################################
then
sed -e '/^FROM my[.]repo[.]org/{p;d}' -e 's/^FROM /FROM my.repo.org\//' file.txt
gives output
FROM my.repo.org/3.8.7-eclipse-temurin-19
...
###############################################
FROM alpine:3.14
...
################################################
Explanation: I give 2 instruction to GNU sed
, if line starts with FROM my.repo.org
(observe that I use [.]
to denote literal dot, not any character) then it should print as is (p
) and proceed to next line (d
) therefore such line will never be altered and to replace staring FROM
using FROM my.repo.org/
, note that /
needs to be escaped to avoid being considering delimiter.
(tested in GNU sed 4.8)
Answered By - Daweo Answer Checked By - Mary Flores (WPSolving Volunteer)