137

I want to grep smb.conf and see only lines which are not commented.

denys
  • 1,649

9 Answers9

223
grep "^[^#;]" smb.conf

The first ^ refers to the beginning of the line, so lines with comments starting after the first character will not be excluded. [^#;] means any character which is not # or ;.

In other words, it reports lines that start with any character other than # and ;. It's not the same as reporting the lines that don't start with # and ; (for which you'd use grep -v '^[#;]') in that it also excludes empty lines, but that's probably preferable in this case as I doubt you care about empty lines.

If you wanted to ignore leading blank characters, you could change it to:

grep '^[[:blank:]]*[^[:blank:]#;]' smb.conf

or

grep -vxE '[[:blank:]]*([#;].*)?' smb.conf

Or

awk '$1 ~ /^[^;#]/' smb.conf
goldilocks
  • 87,661
  • 30
  • 204
  • 262
  • 1
    right answer is: cat /etc/samba/smb.conf | grep ^[^#\;] But anyway Thank you – denys Jan 11 '13 at 20:09
  • Yeah, or you could use "quotes"; I edited that in subsequently. – goldilocks Jan 11 '13 at 20:11
  • or with quotes - as you just corrected :) – denys Jan 11 '13 at 20:11
  • 7
    @denys You're wrong: goldilocks's response in not worst than your. .1 Please avoid using cat ...| syntax!. .2 For whipping empty lines AND lines containing only space, maybe with comments U could use this: grep -v "^ *\(#.*\|\)$" < smb.conf – F. Hauri - Give Up GitHub Jan 11 '13 at 20:14
  • @denys For samba, did you try testparm command? – F. Hauri - Give Up GitHub Jan 11 '13 at 20:15
  • @F.Hauri: What's wrong with cat ... |? – Emanuel Berg Jan 11 '13 at 21:51
  • 8
    @EmanuelBerg It's an useless fork. cat file | grep "blah" implie running two binaries through a fifo, while grep "blah" <file do exactly same and bind file naturaly to grep's STDIN . [bash] useless cat is a full featured subject of search through any search engine! -> http://blog.sanctum.geek.nz/useless-use-of-cat/ ... for sample – F. Hauri - Give Up GitHub Jan 11 '13 at 22:01
  • @F.Hauri: If it does exactly the same, how can it be useless? – Emanuel Berg Jan 11 '13 at 22:08
  • 2
    It doesn't do exactly the same. It creates 2 processes and a pipe where 1 process is enough. Read the link given in @F.Hauri's last comment. – rahmu Jan 11 '13 at 22:12
  • @rahmu: Do you habitually run out of processes? That syntax is intuitive and there is nothing wrong using it. – Emanuel Berg Jan 12 '13 at 00:02
  • @EmanuelBerg If you use it, you will be attacked by a velociraptor. On a more serious note, you can use the <filename command syntax which has the merits of both; you don't use cat and you get the filename before the command. – wizzwizz4 Mar 19 '17 at 22:04
  • "Useless use of cat" is not intuitive -- I used to do it a lot. It's habitual. People new to the command line who learn to use grep correctly will not find it "counter-intuitive" vs. useless use of cat but, I think, more likely vice versa, because the issue is a pretty drop dead simple one to understand. There is no possible advantage to useless use of cat unless you've become habituated to doing it (i.e., you have a bad habit!). – goldilocks Mar 22 '17 at 12:22
  • I did a similar thing to look for lines not starting with a quote, but '^[^"]' found the first line of the file, which did start with a quote! Does ^ only work if there is a previous line? – Michael Jan 19 '22 at 18:00
5

These examples might be of use to people.

[user@host tmp]$ cat whitespacetest
# Line 1 is a comment with hash symbol as first char

# Line 2 is a comment with hash symbol as second char
  # Line 3 is a comment with hash symbol as third char
        # Line 4 is a comment with tab first, then hash
        ; Line 5 is a comment with tab first, then semicolon. Comment char is ;
; Line 6 is a comment with semicolon symbol as first char
[user@host tmp]$

The first grep example excludes lines beginning with any amount of whitespace followed by a hash symbol.

[user@host tmp]$ grep -v '^[[:space:]]*#' whitespacetest

        ; Line 5 is a comment with tab first, then semicolon. Comment char is ;
; Line 6 is a comment with semicolon symbol as first char
[user@host tmp]$

The second excludes lines beginning with any amount of whitespace followed by a hash symbol or semicolon.

[user@host tmp]$ grep -v '^[[:space:]]*[#;]' whitespacetest

[user@host tmp]$
5

The pipe to grep in oliver nadj's answer may be eliminated by (assuming GNU grep or compatible):

grep -v "^\s*[#\;]\|^\s*$" <some_conf_file>
5

Vim solution:

:v/^\s*[#\n]/p

I stumbled across this question when trying to find the vim solution myself.

  • 1
    This would also delete lines that starts with blanks. – Kusalananda Jun 07 '19 at 14:57
  • True. I found that useful for config files as lines between comments otherwise fill up the output. If you'd rather keep the blank lines, change the * to + like so: v/^\s+[#\n]/p – David Lord Jun 08 '19 at 23:27
3
grep -v "^\s*[#;]" any.conf | grep -v "^\s*$"

that is what works for me. ignore commented or empty lines, even whitespace before hash mark or semicolon

muru
  • 72,889
0

Here i got better one (assuming GNU grep or compatible):

grep -v '^[#;/%<]\|^\s*$' anyfile.conf

exclude for lines which begins with #;/%< which are in square brackets and the second filter after pipe is \s*$ for blank lines.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • 2
    In what type of conf files are < or / used for comments? I know a lot of conf file formats where < is everything but a comment. ! is sometimes used like in X resource files. – Stéphane Chazelas Mar 16 '17 at 10:42
-1
grep -v '^$\|^\s*#' temp

This one's a lot better, I got it from https://stackoverflow.com/questions/17392869/how-to-print-a-file-excluding-comments-and-blank-lines-using-grep-sed

It assumes GNU grep or compatible.

Tek
  • 171
-2
egrep -v "^#|^$" anyfile.txt

this command is to grep all info in file excluding comments and blank lines.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
-3

This should display you the file without those lines that begin with #: grep -v "^#" filename

Philippos
  • 13,453