-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.

  • 1
    `cp originalFile original.back && awk '{ .... }' originalFile > output.tmp && mv output.tmp originalFile` – αғsнιη Apr 26 '21 at 23:08
  • See also [How to edit next line after pattern using sed?](https://unix.stackexchange.com/questions/285160/how-to-edit-next-line-after-pattern-using-sed) – steeldriver Apr 26 '21 at 23:15

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,075
  • 2
  • 52
  • 68
0

Looks like this works:

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