-1

I have a YAML file, schema.yml, with some values ​​like:

{{CODE}}-project
{{CODE}}-project-start

If I use

REP="s/{{CODE}}-project/123-project"
cat schema.yml | sed "$REP"

it will replace both {{CODE}}-project and {{CODE}}-project-start. How can I replace only {{CODE}}-project occurrences but not {{CODE}}-project-start occurrences?

4
  • 1
    Does that help? REP="s/{{CODE}}-project$/123-project/" Commented Sep 13, 2023 at 20:08
  • See: The Stack Overflow Regular Expressions FAQ Commented Sep 13, 2023 at 20:12
  • @tink Did you look at the answers there? They use \< and \>, but this will match before - so it won't solve this problem. Commented Sep 13, 2023 at 20:21
  • The links do not solves this issue... Commented Sep 13, 2023 at 21:22

1 Answer 1

0

Use anchors:

sed 's/^{{CODE}}-project$/123-project/' file

Or skip the substitution if something else is matched on the line; in this case -start:

sed '/-start/! s/^{{CODE}}-project/123-project/' file

Or Perl with a negative lookahead:

perl -lpe 's/^{{CODE}}-project(?!-start)/123-project/' file

All print:

123-project
{{CODE}}-project-start

Btw: using cat is not necessary to feed input into sed. Better to read the file directly.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.