Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Using grep and sed to make changes to a config.yaml file

Writer Matthew Barrera

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: ""
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: ""
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: "" scope_id: "{scope_id}" attestation: method: "tpm" registration_id: "{registration_id}"
DPS symmetric key provisioning configuration
provisioning: source: "dps" global_endpoint: "" 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

1 Answer

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

0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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