ecl0
ecl0

Reputation: 435

How to replace a string with string containing multiple / characters

I am trying to change the following string

FROM java_jre_8@sha256:92f22331226b9b3c43a15eeeb304dd7

to

FROM docker-registry.service.consul:5000/java_jre_8@sha256:92f22331226b9b3c43a15eeeb304dd7 

but am having difficult with sed as a result of / character

This is for a build server.

Upvotes: 0

Views: 70

Answers (2)

Maroun
Maroun

Reputation: 95958

You can use # as the delimiter as it doesn't appear in your string (you can still use / but then you'll have to quote the actual /s that are part of the string).

sed "s#FROM java_jre_8@#FROM docker-registry.service.consul:5000/java_jre_8@#'

Example:

$ echo "FROM java_jre_8@sha256:92f22331226b9b3c43a15eeeb304dd7" | sed "s#FROM java_jre_8@#FROM docker-registry.service.consul:5000/java_jre_8@#"
FROM docker-registry.service.consul:5000/java_jre_8@sha256:92f22331226b9b3c43a15eeeb304dd7

Upvotes: 0

HardcoreHenry
HardcoreHenry

Reputation: 6387

There are two ways of doing this. The first is to escape each / in the string you're replacing:

sed 's/from/to with \/ ... /'

The other, more simple way is to use a delimiter other than /. While most sed examples use / as a delimiter, you can use any character:

sed 's|from|to with / ...|'

Here, the | is the first character following s, and therefore sed knows to use this as a delimiter.

Upvotes: 1

Related Questions