A simpler solution that avoids unneeded loops, using Mac OS X's BSD sed1:
/# begin/,/# end/ { # Search only within this range of text.
//!s/^# // # Excluding range borders, delete /^# / from each line.
}
Testing:
▶ cat > FILE <<EOF
aaa
bbb
# begin
# [block]
# param1=""
# param2=""
# end
ccc
ddd
EOF
▶ sed -i '' '/# begin/,/# end/{//!s/^# //;}' FILE
▶ cat FILE
aaa
bbb
# begin
[block]
param1=""
param2=""
# end
ccc
ddd
Further explanation:
- Note that the empty regular expression
//
repeats the last regular expression match. Thus, //!
negates that. And /PAT1/,/PAT2/{ //! X }
has the effect of excluding both /PAT1/
and /PAT2/
for X
.
1 And also tested using GNU sed.