Thursday, October 28, 2021

[SOLVED] Sed regular expression not matching in configuration

Issue

I have a configuration file where I am trying to replace a particular piece of text. The regex isn't matching though.

Sample config text:

containers:
  - name: container-name
    image: docker-registry.prod.com:5000/project:5f9775ae7580e5c06b8c929b91501f722c5d6d99
    ports:
    - containerPort: 80

I'm trying to match and replace the project:<sha> text.

What I've been tring (using ### for the sample):

sed -i '' 's|\/project\:[a-z0-9]+|\/###|g' manifest.yml


Solution

To use + (one or more repetition operator), you need to enable the extended regular expression matching (ERE) with -E flag (or -r in GNU sed). By default sed will use BRE:

sed -E -i '' 's|/project:[a-z0-9]+|/###|g' manifest.yml

Also, when using alternative separator in s command (here |), you don't need to escape the slash (nor colon).



Answered By - randomir