Pete
Pete

Reputation: 113

Is it possible to collapse these two grep queries into a single line command?

I want to download the source of a web page and then check for the presence of one string, and the absence of another. I have working code below - is it possible to do this entire script as a one-line command?

PAGE_SOURCE=`curl https://example.com`

if ! echo "$PAGE_SOURCE" | grep -q "string that is bad"; then
    if echo "$PAGE_SOURCE" | grep -q "string that is good"; then
        exit 0
    fi
fi

exit 1

Upvotes: 1

Views: 180

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

You may use

if grep -Pq '^(?!.*string that is bad).*string that is good'; then
    exit 0
fi

The well-known PCRE pattern described in Regular expression for a string containing one word but not another thread should be put inside a single-quoted string, or you will need to escape ! char since it is used to expand history.

The short pattern description:

  • ^ - start of string
  • (?!.*string that is bad) - a negative lookahead that fails the match if there are any 0+ chars other than line break chars as many as possible and then string that is bad immediately to the right of the current location
  • .*string that is good - any 0+ chars other than line break chars as many as possible and then string that is good.

Upvotes: 1

Related Questions