grep은 다른 문자열이 앞에 오지 않는 문자열을 찾습니다.

grep은 다른 문자열이 앞에 오지 않는 문자열을 찾습니다.

주석 처리된 줄을 무시하거나 한 문자열만 일치시키고 다른 문자열은 일치시키지 않는 방법에 대한 많은 가이드를 찾았지만 아직 필요한 것을 찾지 못했습니다. 문자열 "indexes"(대소문자 구분 안 함, apacheconf임)가 포함되어 있지만 앞에 "#"이 없는 파일을 재귀적으로 찾고 싶습니다.

다음과 일치해야 합니다.

Options Indexes
Options +Indexes
Options Indexes MultiViews
Options Indexes # Comment
Options Indexes # Indexes

하지만 이건 아니다:

Options MultiViews # Indexes
# Indexes yadayada Indexes

내 양식의 스크립트에서 이것을 사용합니다.

if grep -re "[^#]*ndexes" $DIR1/httpd.conf $DIR2/http; then
    echo Do not use Indexes
fi

위의 내용은 내 노력 중 하나이지만 제대로 작동하지 않습니다.

답변1

다음 입력이 주어지면:

Options Indexes
Options +Indexes
Options Indexes MultiViews
Options Indexes # Comment
Options Indexes # Indexes
Options MultiViews # Indexes
# Indexes yadayada Indexes

이것은 작동하는 것 같습니다:

$ grep '^[^#]*Indexes' input
Options Indexes
Options +Indexes
Options Indexes MultiViews
Options Indexes # Comment
Options Indexes # Indexes
$ grep -v '^[^#]*Indexes' input
Options MultiViews # Indexes
# Indexes yadayada Indexes

정규식 분석:

  • ^- 줄의 시작
  • [^#]*- Octothorpe 이외의 문자가 0개 이상 포함되어 있습니다.
  • Indexes- 리터럴 문자열Indexes

스크립트의 컨텍스트에 넣으십시오.

if grep -rl -- '^[^#]*Indexes' "$DIR1/httpd.conf" "$DIR2/http"; then
    echo "The above-listed files use an 'Indexes' directive."
fi

관련 정보