Skip to content


How to view a configuration file without the comments

Linux

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.

Posted in Linux.

Get Simple Help tutorials just like this one in your email inbox every day - for free! Just enter your email address below:

 

You can always opt out of this email subscription at any time.

5 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. paulfxh says

    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

  2. paulfxh says

    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. paulfxh says

    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. lxtips says

    hi, is this the same as:

    grep -v ‘#’ file

Continuing the Discussion

  1. Display files without commented-out lines « Linux & Stuff linked to this post on May 31, 2009

    [...] in CLI, Uncategorized | Tags: CLI This SimpleHelp tip could be very [...]



Some HTML is OK

or, reply to this post via trackback.