Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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 want to grep smb.conf and see only lines which are not commented.

share|improve this question
1  
testparm do this better as this display default values too. – F. Hauri Jan 11 '13 at 20:08
    
@F.Hauri Thank you. it is working as well. But it is also produce some unneeded (in my case) information, which should be grepped also... – denys Jan 11 '13 at 20:21
    
up vote 32 down vote accepted
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 ;.

share|improve this answer
    
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
4  
@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 Jan 11 '13 at 20:14
6  
@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! -> blog.sanctum.geek.nz/useless-use-of-cat ... for sample – F. Hauri Jan 11 '13 at 22:01
1  
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

The pipe to grep in oliver nadj's answer may be eliminated by

grep -v "^\s*[#\;]\|^\s*$" <some_conf_file>
share|improve this answer
    
Second grep i@nadj's looks useless , @goldilocks's answer was more than eough. – Archemar Oct 13 '15 at 9:19
1  
I agree that goldilocks's answer was correct. However, I also considered it useful to not show empty lines. – Morten Lind Oct 13 '15 at 17:41

Vim solution:

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

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

share|improve this answer

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]$
share|improve this answer
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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.