如何用正则expression式匹配/黑名单?

我见过这个问题: 正则expression式匹配一个不包含单词的行吗?

但是我无法让它工作。 我有一个shell脚本,我正在使用

string1.*string2.*string3 

要按文件顺序search3个单词。 但是我想改变它,所以如果badword5在那个文件中的那些单词之间的任何地方,那么没有正则expression式与grep匹配。

所以这应该匹配:

 ./testing/test.txt: let prep = "select string1, dog from cat", " where apple = 1", " and string2 = 2", " and grass = 8", " and string3 = ?" 

但是这不应该:

  ./testing/test.txt: let prep = "select string1, dog from cat", " where apple = 1", " and string2 = 2", " and grass = 8", " and badword5 = 4", " and string3 = ?" 

我尝试失败:

 string1((?!badword5)|.)*string2((?!badword5)|.)*string3 

整个脚本:

 find . -name "$file_to_check" 2>/null | while read $FILE do tr '\n' ' ' <"$FILE" | if grep -q "string1.*string2.*string3"; then echo "$FILE" ; fi done >> $grep_out 

“按顺序在一个文件中搜索3个单词,但是我想改变它,这样如果badword5在那个文件中的那些单词之间的任何地方,就没有与grep匹配的正则表达式。

事实上,搜索模式延伸了多条线。
让我们暂时放下grep ,尝试一些不同的东西:

 #!/bin/bash find . -name "$file_to_check" 2>/dev/null | while read FILE do SCORE=0 tr ' ' '\n' <"$FILE" | while read WORD do case $WORD in "word1" ) [ $SCORE = 0 ] && SCORE=1 ;; "word2" ) [ $SCORE = 1 ] && SCORE=2 ;; "word3" ) [ $SCORE = 2 ] && echo "$FILE" && break ;; "badword5" ) SCORE=0 ;; esac done done >grep_out 

案例线做以下事情:

 " word1" ) [ $SCORE = 0 ] && SCORE = 1 ;; when word1 is found: and SCORE is equal to 0 then make SCORE equal to 1 when word2 is found: and SCORE is equal to 1 then make SCORE equal to 2 when word3 is found: and SCORE is equal to 2 then print filename and break out of the inner loop. 

您可以使用grep -v跳过badword5

 tr '\n' ' ' < "$FILE" | grep -v 'badword5' | if grep -q "string1.*string2.*string3"; then echo "$FILE" ; fi