Withtaker
Withtaker

Reputation: 1874

Replace String between matching multiline String with sed

I have the following configuration file:

dependencies:
- name: server2
  version: 1.0.0.0
  repository: "repository2"
- name: server1
  version: 4.3.2.1
  repository: "repository2"

I now want to replace the version of server 1 with something like 'newVersion'.

The version is always below the name, I want to replace the version number (4.3.2.1), but I don't know that number, so I have to replace anything that comes between 'version:' and '\n'.

I know that I want to change the version of server1 though.

I tried the following:

sed ':a;N;$!ba;s/server1\n\s\sversion:/newVersion/}' file.txt

I know sed can't 'normally' read multi-lines without the N operator, but it still does not work for me, does anyone could help me here?

Upvotes: 0

Views: 80

Answers (2)

steffen
steffen

Reputation: 16938

This sed replaces between name: server1$ and version: this: version: .* by version: newVersion.

sed '/name: server1$/,/version: /s/version: .*/version: newVersion/'

Upvotes: 0

PesaThe
PesaThe

Reputation: 7499

This sed should do:

$ sed '/name: server1$/{n;s/version: .*/version: 5.4.3.2/}' data
dependencies:
- name: server2
  version: 1.0.0.0
  repository: "repository2"
- name: server1
  version: 5.4.3.2
  repository: "repository2"

Upvotes: 1

Related Questions