0

I have found lots of guides for ignoring lines with comments or matching for one string but not another, but I have not found what I need. I want to grep recursively for files containing the string "indexes" (case insensitive i think, it's apache conf) but not a "#" BEFORE it.

It should match this:

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

But not this:

Options MultiViews # Indexes
# Indexes yadayada Indexes

I'm using it in a script on the form:

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

The above is one of my efforts, but I cannot get it to work.

  • 2
    why it should match this Options Indexes # Indexes but not this Options MultiViews # Indexes ? – RomanPerekhrest Jun 20 '17 at 15:21
  • It is a comment but it is also a valid config line – Stephen Rauch Jun 20 '17 at 15:26
  • grep -E '^[^#]*Indexes' should do the trick. – DopeGhoti Jun 20 '17 at 15:27
  • But it doesn't, and it's pretty much exactly what my not-working-example looks like. – klutt Jun 20 '17 at 15:31
  • Ah, it works after your edit. – klutt Jun 20 '17 at 15:31
3

Given this input:

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

This appears to work:

$ 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

To dissect the regular expression:

  • ^ - Beginning of the line
  • [^#]* - Zero or more of any character that is not an octothorpe
  • Indexes - The literal string Indexes

To put it in the context of your script:

if grep -rl -- '^[^#]*Indexes' "$DIR1/httpd.conf" "$DIR2/http"; then
    echo "The above-listed files use an 'Indexes' directive."
fi
  • What is -- good for? – klutt Jun 20 '17 at 15:59
  • -- signifies to many programs that there are no further arguments starting with - forthcoming, which is useful in the possible case that $DIR1 or $DIR2 happen to start with a hyphen. – DopeGhoti Jun 20 '17 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.