2017-02-18 5 views
0

내가그렙 # (해시) 또는 욕심 공간과 # (해시)없이 시작하는 모든 라인

  • 해시 (또는)
  • 로 시작하지 않는 conf의 파일의 유효한 모든 라인을 grep을 위해 노력하고 있어요
  • 공간 (0 또는 많은)의 수 및 정규 표현식에 아래 작동하지 않는 해시

.

grep ^[^[[:blank:]]*#] /opt/logstash/logstash.conf 

grep ^[^[[[:blank:]]*#]] /opt/logstash/logstash.conf 

grep ^[^[\s]*#] /opt/logstash/logstash.conf 

내가 뭘 잘못하고 있는지 이해하려고합니다.

FWIW, 아래 정규식 작동 : 모든

grep -v '^[[:blank:]]*#' /opt/logstash/logstash.conf 

답변

2

첫째, 당신이 당신의 정규식을 인용하지 않는 경우 그렙조차보기 전에 당신은 항상 당신의 그렙 정규 표현식에 인용한다, 그래서 쉘이 그것을 확장 될 수 있습니다 .

둘째, 작동하지 않는 이유는 []이며, "대괄호 식"은 [^...]을 사용할 때 포함 된 요소 중 하나와 일치합니다. 당신은 부정적인 문자 시퀀스와 일치하는 것을 시도하는 것 같지만, 그것은 []이 아닌 것입니다.

또한 대괄호 표현식 내에서 문자 클래스는 [^[:blank:]] 대신 [^[[:blank:]]]이 아닌 대괄호로 묶지 않아야합니다.

가 나는 -v 그냥 기본 또는 확장 정규 표현식없이 가능하다면 모르겠지만, (예 : GNU의 GREP 등) 펄 정규 Epxressions을 할 수있는 그렙, 당신은 부정적인 예견을 사용할 수

여기
grep -P '^(?![[:blank:]]*#)' /opt/logstash/logstash.conf 

은 정규 표현식에가 (그들이 인용하고 가정) 실패하는 방법입니다

  • ^[^[[:blank:]]*#] –이보다 0 개 이상의 문자와 기타를 시작하는 라인을 일치 리터럴 [ 또는 다음, 빈
  • ^[^[[[:blank:]]*#]]#]가 아닌 0 개 이상의 문자로 시작하는 라인과 일치하는 문자 [ 또는 #]]
  • ^[^[\s]*#] – 일치 하였다 (브래킷 식에서 반복 [[ 단일 [와 동일) 빈 [, \ 또는 s (\s[]에는 특별하지 않음) 이외의 문자로 시작하는 줄과 그 뒤에 #]이 오는 줄.우리는이 테스트 파일이 걸릴 경우

는 :

# comment 
normal line 
    normal line after spaces 
    # comment after spaces 
    normal line after tab 
    # comment after tab 
abc#] does not start with blank or [ and has #] 
[abc#] starts with [ and has #] 
abc#]] does not start with blank or [ and has #]] 
abc#]] starts with blank and has #]] 
sabc#]] starts with s and has #]] 
\abc#]] starts with \ and has #]] 

표현식 매치를 다음과 같이

  • 귀하의 grep -v (작품) :

    $ grep -v '^[[:blank:]]*#' infile 
    normal line 
        normal line after spaces 
        normal line after tab 
    abc#] does not start with blank or [ and has #] 
    [abc#] starts with [ and has #] 
    abc#]] does not start with blank or [ and has #]] 
    abc#]] starts with blank and has #]] 
    sabc#]] starts with s and has #]] 
    \abc#]] starts with \ and has #]] 
    
  • grep -P (작품) :

    $ grep -P '^(?![[:blank:]]*#)' infile 
    normal line 
        normal line after spaces 
        normal line after tab 
    abc#] does not start with blank or [ and has #] 
    [abc#] starts with [ and has #] 
    abc#]] does not start with blank or [ and has #]] 
    abc#]] starts with blank and has #]] 
    sabc#]] starts with s and has #]] 
    \abc#]] starts with \ and has #]] 
    
  • 첫 번째 시도 :

    $ grep '^[^[[:blank:]]*#]' infile 
    abc#] does not start with blank or [ and has #] 
    abc#]] does not start with blank or [ and has #]] 
    sabc#]] starts with s and has #]] 
    \abc#]] starts with \ and has #]] 
    
  • 두 번째 시도 :

    $ grep '^[^[[[:blank:]]*#]]' infile 
    abc#]] does not start with blank or [ and has #]] 
    sabc#]] starts with s and has #]] 
    \abc#]] starts with \ and has #]] 
    
  • 세 번째 시도 :

    $ grep '^[^[\s]*#]' infile 
    abc#] does not start with blank or [ and has #] 
    abc#]] does not start with blank or [ and has #]] 
    abc#]] starts with blank and has #]]