How to view a configuration file without the comments

I have to often make changes to configuration files such as httpd.conf and squid.conf. These files have a large number of lines that are commented out, mostly comments and some possible configuration directives that have been commented out as they are not required by default. A problem I face while editing such files is that there are so many lines commented out that I need to scroll down many lines before I can find the next active configuration directive. I found a fine solution to help me out with this.

I now use the following command when I want to just look at the active directives in the Apache configuration file:

# sed ‘/ *#/d; /^ *$/d’ /etc/httpd/conf/httpd.conf

This command reads the file /etc/httpd/conf/httpd.conf and filters out all the comments and extra white spaces, leaving just the active configuration settings. This makes it very easy for me to look at the configuration file.

If you want to just filter out the lines that begin with comments run the following command:

# sed ‘/ ^#/d; /^ *$/d’ /etc/httpd/conf/httpd.conf

It’s pretty much the same command as earlier with a small change. The first * is replaced by ^, which is regex for beginning of line.

6 thoughts on “How to view a configuration file without the comments”

  1. Your command to show a file without comments could be very useful for me.
    However, when I tried to run
    # sed ‘/ ^#/d; /^ *$/d’ /boot/grub/menu.lst
    I get this error
    sed: -e expression #1, char 1: unknown command: `�'
    bash: /^: No such file or directory
    Note that I copied/pasted the command directly from your post.
    I have a feeling this may be a HTML-related problem.
    Any clues?

  2. Well, after a bit of searching around, I found that this command does what I want
    sed '/^#/ d' /boot/grub/menu.lst>/boot/grub/
    Still not sure exactly what’s wrong with the command given in the article, but it might be that the HTML has somehow ‘distorted’ the first single quote mark

  3. After a little more experimentation, I find that the command below does what the command in the article was intended to do
    sed '/^#/d; /^*$/d' /path/to/file/

  4. Pingback: Display files without commented-out lines « Linux & Stuff

Leave a Comment

Your email address will not be published. Required fields are marked *