How to View a Configuration File Without the Comments

This guide will give you a couple of options/suggestions on how to view a config file in Linux 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 (sed) 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.

Update: see several of of the helpful comments below if these commands don’t allow you to properly view a config file without the comments.


If this article helped you, I'd be grateful if you could share it on your preferred social network - it helps me a lot. If you're feeling particularly generous, you could buy me a coffee and I'd be super grateful :)

buy a coffee for simplehelp.net


Home » Linux » How to View a Configuration File Without the Comments

5 thoughts on “How to View a Configuration File Without the Comments”

  1. 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/

  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/menu.new
    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. 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?
    Thanks
    Paul

Leave a Comment

Your email address will not be published.