Trim Clean and Make order with Sed

Configuration files for many programs can become difficult to read and understand. Whitespace and comments cloud the real configuration settings. Sed is a wonderful tool to work on files. Here are some guides but do remember to save a copy of any file first. There are many books on Sed and its related tools, this is a guide about using Sed for cleaning configuration files. The inline action of Sed will read and update a file inline. Additions to this guide will be made over time. We will clean up some Asterisk configuration files
cd /etc/asterisk
Make backups
mkdir /usr/src/`date +%Y%m%d` && cp * /usr/src/`date +%Y%m%d`/
Removes beginning and ending white space
sed -i 's/^[ \t]*//;s/[ \t]*$//' *.conf
Deletes empty lines
sed -i '/^$/d' *.conf
Adds a line return above a [
sed -i '/^\[/{x;p;x;}' *.conf
Removes all type = peer entries
sed -i '/type = peer/d' users.conf
Adds a single type peer under a user (not a trunk)
sed -i '/\[[0-9]*\]/ a\type = peer' users.conf
Optional remove comments Deletes comments that start with #
sed -i '/^\#/d' *.conf
Deletes comments that starts with ; at the beginning of a line
sed -i '/^\;/d' *.conf
Deletes comments after the ; at any place (be careful...)
sed -i 's/;.*//' *.conf
Reload configuration files in Asterisk
# touch /etc/asterisk/* && asterisk -rx "reload"