gol*_*cks 179
grep "^[^#;]" smb.conf
Run Code Online (Sandbox Code Playgroud)
第一个^指的是行的开头,因此不会排除在第一个字符之后开始注释的行。 [^#;]表示不是#或 的任何字符;。
换句话说,它报告以除#and以外的任何字符开头的行;。这与报告不以#and开头的行;(您将使用的行grep -v '^[#;]')不同,因为它还排除了空行,但在这种情况下这可能更可取,因为我怀疑您是否关心空行。
如果您想忽略前导空白字符,您可以将其更改为:
grep '^[[:blank:]]*[^[:blank:]#;]' smb.conf
Run Code Online (Sandbox Code Playgroud)
或者
grep -vxE '[[:blank:]]*([#;].*)?' smb.conf
Run Code Online (Sandbox Code Playgroud)
或者
awk '$1 ~ /^[^;#]/' smb.conf
Run Code Online (Sandbox Code Playgroud)
小智 5
这些例子可能对人们有用。
[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]$
Run Code Online (Sandbox Code Playgroud)
第一个 grep 示例排除以任意数量的空格开头,后跟哈希符号的行。
[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]$
Run Code Online (Sandbox Code Playgroud)
第二个排除以任意数量的空格开头,后跟哈希符号或分号的行。
[user@host tmp]$ grep -v '^[[:space:]]*[#;]' whitespacetest
[user@host tmp]$
Run Code Online (Sandbox Code Playgroud)
小智 5
oliver nadj 的答案中的 grep 管道可以通过以下方式消除(假设 GNUgrep或兼容):
grep -v "^\s*[#\;]\|^\s*$" <some_conf_file>
Run Code Online (Sandbox Code Playgroud)