I want to grep smb.conf and see only lines which are not commented.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
Vim solution:
:v/^s*[#n]/p
I stumbled across this question when trying to find the vim solution myself.
Method 3
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>
Method 4
These examples might be of use to people.
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b2e283e291b3334282f">[email protected]</a> 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
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="106563756250787f6364">[email protected]</a> tmp]$
The first grep example excludes lines beginning with any amount of whitespace followed by a hash symbol.
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9bcbaacbb89a1a6babd">[email protected]</a> 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
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3441475146745c5b4740">[email protected]</a> tmp]$
The second excludes lines beginning with any amount of whitespace followed by a hash symbol or semicolon.
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0b5b3a5b280a8afb3b4">[email protected]</a> tmp]$ grep -v '^[[:space:]]*[#;]' whitespacetest [<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2752544255674f485453">[email protected]</a> tmp]$
Method 5
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
Method 6
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.
Method 7
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.
Method 8
egrep -v "^#|^$" anyfile.txt
this command is to grep all info in file excluding comments and blank lines.
Method 9
This should display you the file without those lines that begin with #:
grep -v "^#" filename
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0