2

I have a config.yaml file which i want to edit or make changes to using a bash script. So i got a command using grep and sed which gives me the desired changes , but the problem is that when i apply sed it gets applied to whole file including the section i want to edit. Following is the section of file before editing:

    ###############################################################################
29  
30  # Manual provisioning configuration
31  # provisioning:
32  #  source: "manual"
33  #  device_connection_string: ""
34  
35  # DPS TPM provisioning configuration
36  # provisioning:
37  #   source: "dps"
38  #   global_endpoint: "https://global.azure-devices-provisioning.net"
39  #   scope_id: "{scope_id}"
40  #   attestation:
41  #     method: "tpm"
42  #     registration_id: "{registration_id}"
43  
44  # DPS symmetric key provisioning configuration
45  # provisioning:
46  #   source: "dps"
47  #   global_endpoint: "https://global.azure-devices-provisioning.net"
48  #   scope_id: "{scope_id}"
49  #   attestation:
50  #     method: "symmetric_key"
51  #     registration_id: "{registration_id}"
52  #     symmetric_key: "{symmetric_key}"
53  
54  ###############################################################################

Now i want to edit from "# DPS TPM provisioning configuration" to "# registration_id: "{registration_id}"" at line 42. so i use the following command:

grep -Pzom 1 "# DPS TPM provisioning configuration(.|\n)*?(?=\n# DPS)" config.yaml | sed 's/^#[ \t]//' config.yaml

which gives me following output:


Manual provisioning configuration
provisioning:
  source: "manual"
  device_connection_string: ""

DPS TPM provisioning configuration
provisioning:
  source: "dps"
  global_endpoint: "https://global.azure-devices-provisioning.net"
  scope_id: "{scope_id}"
  attestation:
    method: "tpm"
    registration_id: "{registration_id}"

DPS symmetric key provisioning configuration
provisioning:
  source: "dps"
  global_endpoint: "https://global.azure-devices-provisioning.net"
  scope_id: "{scope_id}"
  attestation:
    method: "symmetric_key"
    registration_id: "{registration_id}"
    symmetric_key: "{symmetric_key}"

###############################################################################

This edits the required part but along with that the whole file as well.I just want to apply sed on grep output of the file only and not the entire file. Can anyone help with the command please!

  • 2
    Are those line numbers in the actual text? If not: sed '35,42 s/#//' config.yaml should serve. – andrew.46 Oct 4 '19 at 4:42
  • thanks @andrew.46 for replying. No those line numbers were from cat command, just for reference and i don't want to edit on line numbers because it might change hence the regex – Prototype Oct 4 '19 at 5:35
1

try:

sed '
    /# DPS TPM provisioning configuration/,/#     registration_id: "{registration_id}"/{s/^#//}
' config.yaml

this uncomment every lines start with # by s/^#// within lines matching ranges specified in //,//.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.