grep smb.conf
하고 주석이없는 줄 만보 고 싶습니다 .
답변
grep "^[^#;]" smb.conf
첫 번째 ^
는 줄의 시작을 나타내므로 첫 번째 문자 다음에 시작되는 주석이있는 줄은 제외되지 않습니다. 또는 [^#;]
아닌 문자를 의미합니다 .#
;
즉, #
및 이외의 다른 문자로 시작하는 행을보고합니다 ;
. 빈 줄 도 제외한다는 점에서 시작하지 않는 ( #
및 ;
사용하는 grep -v '^[#;]'
) 줄을보고하는 것과 같지 않지만이 경우 빈 줄이 신경 쓰이는 것을 의심하므로 바람직합니다.
선행 공백 문자를 무시하려면 다음과 같이 변경할 수 있습니다.
grep '^[[:blank:]]*[^[:blank:]#;]' smb.conf
또는
grep -vxE '[[:blank:]]*([#;].*)?' smb.conf
또는
awk '$1 ~ /^[^;#]/' smb.conf
답변
Vim 솔루션 :
:v/^\s*[#\n]/p
vim 솔루션을 스스로 찾으려고 할 때이 질문을 우연히 발견했습니다.
답변
oliver nadj의 답변에서 grep 할 파이프는 (GNU grep
또는 호환 가능 하다고 가정) 제거 할 수 있습니다 .
grep -v "^\s*[#\;]\|^\s*$" <some_conf_file>
답변
grep -v "^\s*[#;]" any.conf | grep -v "^\s*$"
그것이 저에게 효과적입니다. 해시 마크 또는 세미콜론 앞의 공백조차도 주석 처리되었거나 빈 줄은 무시하십시오.
답변
이 예제는 사람들에게 유용 할 수 있습니다.
[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]$
첫 번째 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]$
두 번째는 공백으로 시작하고 해시 기호 또는 세미콜론으로 시작하는 행은 제외합니다.
[user@host tmp]$ grep -v '^[[:space:]]*[#;]' whitespacetest
[user@host tmp]$
답변
여기에 더 나은 것이 있습니다 (GNU grep
또는 호환 가능 하다고 가정 ).
grep -v '^[#;/%<]\|^\s*$' anyfile.conf
#;/%<
꺾쇠 괄호 안에있는 줄로 시작하고 파이프 뒤의 두 번째 필터는 \s*$
빈 줄입니다.
답변
grep -v '^$\|^\s*#' temp
이것은 훨씬 낫습니다 .https : //stackoverflow.com/questions/17392869/how-to-print-a-file- except-comments- and- blank-lines-using-grep-sed 에서 얻었습니다.
GNU grep
또는 호환되는 것으로 가정합니다 .