Comment and uncomment files with sed

:heavy_exclamation_mark: This post is older than a year. Consider some information might not be accurate anymore. :heavy_exclamation_mark:

If you have the need to comment a whole file sed is very handy to comment and uncomment files.

sed -i 's/^\([^#]\)/#\1/g' /etc/monit/elasticsearch

To uncomment remove all # at the beginning of each line.

sed -i 's/^#//g' /etc/monit/elasticsearch
Please remember the terms for blog comments.