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.

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

1 Answer 1

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
2
  • What is -- good for? Commented Jun 20, 2017 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. Commented Jun 20, 2017 at 16:10

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.