Search code examples
linuxbashsedyaml

Sed command can't find value with underscore


I'm trying to change values in yaml file using sed command (can't use yq). I have a file test.yaml with following:

  app1:
    value1: 0
    value2: 7
    value_3: 5
  app2:
    value1: 1
    value2: 6
    value_3: 0

With the following command I can change value2 of app2 to 0:

sed -i '/app2/ {n;N;s/value2: 6/value2: 0/}' test.yaml

But for some reason I can't change value_3 with the same command:

sed -i '/app2/ {n;N;s/value_3: 0/value_3: 6/}' test.yaml

No error, just nothing changes.

Is there a known bug of sed or some misunderstanding from my side? Please advice.

I tried to use double quotes or backslashes before _ - no luck.


Solution

  • With your example I suggest GNU sed:

    sed '/\bapp2:/,/\bvalue2:/{ s/\bvalue2: .*/value2: 0/ }' file.yaml
    

    \b: marks a word boundary

    See: The Stack Overflow Regular Expressions FAQ