To remove all lines that begin with a # and then delete all blank lines sed '/^#/d; /^$/d' dovecot-ldap.conf sed -r '/^#/d; /^ +#/d; /^$/d; s/^/ /' dovecot.conf # insert 5 blank spaces at beginning of each line (make page offset) sed 's/^/ /' http://www.eng.cam.ac.uk/help/tpl/unix/sed.html