-1

What I am looking to do is find a match and then replace everything after character in the next line.

Example file contents:

  super_service:
    app_version: 1.02
  duper_service:
    app_version: 4.0.1
  happy_service:
    app_version: 2.03
  turbo_service:
    app_version: 1.28.0

Expected outcome: What I am looking to do is match duper_service for example, and then replace everything after its' "app_version: " with a new version I define, 4.0.2 for example. I am able to do this with awk but need to write the change to the file.

2 Answers2

0

sed seems a good candidate for this:

sed -i '/^[[:space:]]*duper_service:/ { N; s/[^:]*$/ 4.0.2/ }' file.txt

Explanation: when the "duper_service:" line is encountered, next line is appended and whatever ends that line, after the colon, ([^:]*$) is replaced with "4.0.2".

The -i option changes the file in-place. It's a GNU sed extension.

xhienne
  • 17,793
  • 2
  • 53
  • 69
0

Looks like this works:

sed -i '/duper_service/!b;n;c\ \ \ \ app_version: 4.0.2' file.txt