Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some long configuration files to go through, and I would like to see just what is actually active in the .conf file, without any of the # tags. What can I use to output the lines without a # on it? I'm running Debian Wheezy with this command.

share|improve this question
up vote 2 down vote accepted

If the file is named foo.conf:

grep -E '^[^#].*' foo.conf should do it.


-E: Support extended regular expressions!

'^[^#].*': A regular expression surrounded by single quotes.

^[^#].*: The regular expression itself.

^(at position 0 of the regular expression): Says "Match starting at the beginning of a line / the first character immediately following a newline, or the first character of the file itself."

[^#]: Says "Match exactly one character that is not the character #."

.*: Says "Match zero or more of any characters except a newline, for the rest of the line."

The net effect is that, if you have a file with contents like the following:


This regular expression will fail to match the first and third lines, because the first character at the start of lines 1 and 3 is in fact a #, so the part of the regular expression that requires exactly one non-# ([^#]) fails to match, so grep excludes that line.

The regular expression will then succeed to match the remainder of the lines, because the first character at the start of lines 2 and 4 is indeed not a #.

Building on our success so far, you can also match lines such as:

    #I am tricky!

(Notice that there is whitespace (tabs or spaces) in front of the comment, and since it's still a comment, we don't want it!)

by using the following:

grep -P '^\s*+[^#].*' foo.conf

(but the .* is not strictly required; I know, I know.)

So now we have:

-P: Support Perl-compatible regular expressions! (Hint: may not be available universally in all versions/implementations of grep, but it at least is available for a while now in GNU Grep.)

\s*+: The little bit of added regex that says, "Match zero or more whitespace characters, meaning spaces or tabs, and if you do see them, you MUST eat them." This latter part is very important, because without the possessive quantifier *+, the space could match as part of the [^#], which would trick the regular expression. Possessive quantifiers do not exist in POSIX Compatible (Basic or Extended) regular expression flavors, so we have to use PCRE here. There may be a way to do this without a possessive quantifier, but I am not aware of it.

share|improve this answer
1) you don't need extended grep for this, the expression is "non-extended". 2) this will only remove lines where the comment is the first character, which is not part of the requirements 3) the .* is unnecessary in this case, though is useful sometimes with grep -o – Rich Homolka Aug 20 '13 at 16:09
1) Lesson learned; you are right. 2) Fixed with an update using PCRE and a possessive quantifier. He's looking for lines that are considered by the conf file parser to be comments, not just lines that happen to contain a # somewhere in them. Comments in most files are those lines which start either with a # in the first column, or with a # following some whitespace. 3) Lesson learned, thanks. – allquixotic Aug 20 '13 at 16:26
grep -v '#' file

You need to quote the # because your shell may interpret that as a comment as well.

The grep will remove any line with # on it. This may not be what you want. What may be more useful is removing the comments. You can use sed, or the 'Stream EDitor'. This may give you what you want.

sed -e 's/#.*//' < file | less

The regular expression says "find from a comment character to the end of the line, and remove it" then pipe to less to read it more easily.

If the comments are always in the first column, you can use the method as written by @somequixotic, which only shows lines where there is no comment in the first column.

share|improve this answer
This answer is incorrect, because a line such as olol#olol (or, more practically with a script, a line such as ./ #this does ... will fail to match with this strategy, whereas if you use an extended regular expression as in my answer, it will match successfully. (This comment is based on the text of this answer as of its original revision.) – allquixotic Aug 20 '13 at 16:11
@somequixotic yeah, i thought of that after i posted. I was already editing it. Thanks though. – Rich Homolka Aug 20 '13 at 16:12
Removed my downvote – allquixotic Aug 20 '13 at 16:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .