Issue
I have this YAML file:
openapi: 3.0.0
info:
title: Test
description: >-
This is the official REST API documentation for all available routes
provided by the **Test** plugin.
version: 2.0.0
servers:
- url: https://entwicklungsumgebung
components:
securitySchemes:
basicAuth:
type: http
Inside my CI pipeline, I'm trying to modify the version at line 7. At the middle of the file, there are multiple keys named test_version
or prod_version
which should not be replaced. For this, I've written this sed
command:
sed -i '' 's/^version: .*/version: 3.0.0/' test.yml
Initially, I've not used ^
after s/
but this matched also everything before version. Now, nothing gets replaced. Any idea what I'm doing wrong?
Solution
You have to preserve the whitespace, so you either have to match the entire line and use a capture group:
sed -i '' 's/^\([[:blank:]]*version:\) .*/\1 3.0.0/' test.yml
Or you could match only version
, with the risk of matching where it shouldn't:
sed -i '' 's/\(version:\) .*/\1 3.0.0/' test.yml
A more robust solution would be to use a tool what can parse YAML, such as yq:
yq -i '.info.version = "3.0.0"' test.yml
Answered By - Benjamin W. Answer Checked By - Mary Flores (WPSolving Volunteer)