Issue
I am trying to comment or uncomment a line using Sed in yaml , couldn't find a method using yq.
- files:
force-magic: no # force logging magic on all logged files
# force logging of checksums, available hash functions are md5,
# sha1 and sha256
#force-hash: [md5]
I tried these commands as I have white space sed -i '/<pattern>/s/^[#[:space:]]/#/g' file to comment out, sed -i '/<pattern>/s/^[#[:space:]]//g' file to uncomment But issue is that they are removing space , which make yaml format truncated. Ex :
- files:
force-magic: no # force logging magic on all logged files
# force logging of checksums, available hash functions are md5,
# sha1 and sha256
force-hash: [md5]
I want to comment / uncomment this force-hash line , in such a way that it maintain it yaml properties i.e to be part of files array Expected output
- files:
force-magic: no # force logging magic on all logged files
# force logging of checksums, available hash functions are md5,
# sha1 and sha256
force-hash: [md5]
Solution
sed 's/force-hash/\# force-hash/g' <name_of_file> #this will comment your line
sed 's/#\(force-hash.*\)/\1/' <name_of_file> #to uncomment
Solution
I am sure there is a way with yq
, I just do not have a need to learn it at this time, so here is an option using sed
.
$ sed 's/#\(force-hash.*\)/\1/' input_file
- files:
force-magic: no # force logging magic on all logged files
# force logging of checksums, available hash functions are md5,
# sha1 and sha256
force-hash: [md5]
Answered By - HatLess Answer Checked By - Marilyn (WPSolving Volunteer)