Issue
I try to capture those blocks of strings and remove comment on them using regexp and sed. each block separated with space
some text here
some text here
# AppServer1:
# name: ${AppServer1.name}
# ip: ${AppServer1.ip}
some text here
some text here
# AppServer2:
# name: ${AppServer1.name}
# ip: ${AppServer1.ip}
some text here
some text here
I try with this regexp:
sed '/^AppServer1/I{:a; /^[[:blank:]]*$/!{s/.*#.*/&/; n; ba;} }' file
but it dos not effect the string
what im missing here to UN comment the full string to be :
AppServer1:
name: ${AppServer1.name}
ip: ${AppServer1.ip}
UPDATE After implementing @anubhava solution i notice that if the string is notice the extra "#" in between the blocks:
some text here
some text here
# AppServer1:
# name: ${AppServer1.name}
# ip: ${AppServer1.ip}
#
# AppServer2:
# name: ${AppServer1.name}
# ip: ${AppServer1.ip}
some text here
some text here
And i like to remove only the comments of "AppServer1:" It will remove the comments also from "AppServer2:" and it will look like this :
AppServer1:
name: ${AppServer1.name}
ip: ${AppServer1.ip}
AppServer2:
name: ${AppServer1.name}
ip: ${AppServer1.ip}
This can happen if someone by accident set extra "#" how can i add this OR condition in the regexp so if its blank line OR 1 # in the line dont continue to un comment ?
Solution
You may try this gnu-sed
that doesn't require a blank line after commented lines that start with /# AppServer1/
:
sed -E '/^#[[:blank:]]*AppServer1:/I, /^#?[[:blank:]]*$|^($|[^#])/ s/#[[:blank:]]?//' file
some text here
some text here
AppServer1:
name: ${AppServer1.name}
ip: ${AppServer1.ip}
# AppServer2:
# name: ${AppServer1.name}
# ip: ${AppServer1.ip}
some text here
some text here
Details:
/^#[[:blank:]]*AppServer1:/I
: Start range from a line that starts with#
followed by 0 or more spaces andAppServer1:
(ignore case),/^#?[[:blank:]]*$|^($|[^#])/
: Range end with a line that just has#
OR a line that doesn't have#
at line starts/#[[:blank:]]?//
: Removes first#
followed by an optional space
Answered By - anubhava Answer Checked By - Gilberto Lyons (WPSolving Admin)