Learning to remove extraneous comments from a file with a few deft key strokes
Now that we are the administrator, the Zen master of search and replace, we can use these skills to tidy configuration files that often have many hundreds of commented lines within them. I do not mind documentation but when it becomes such an overwhelming majority, it can take over. Consider the httpd.conf
Apache configuration file under /etc/httpd/conf/
. This has 675 commented lines. We perhaps want to keep the original file as a reference. So let's first make a copy by executing the following command; we know how to do this from the Preface of this book and if you did not read it, now is your chance to read it before a letter goes home to your parents.
# cd /etc/httpd/conf # cp httpd.conf httpd.conf.$(date +%F)
We can easily list the commented lines using the following command that counts the lines that begin with the #
sign, a comment:
# egrep -c '^#' httpd.conf
On my system, we see that there are 675 such lines. Using sed or Vim, we can remove the comments, firstly, with sed, as follows:
# sed -i '/^#/d' httpd.conf
Then, within Vim with the file open, it is a little different:
:g/^#/d
The result is the same in both examples where we have reduced the numbers of lines in the file by about two-thirds.