Regex/Grep 过滤文本文件中的字符串

Regex/Grep to filter strings in text file

我需要一个 regex/grep 命令来处理其中包含多个字符串的文本文件,并且如果存在 String1 和 String2 以外的字符串,我想 return 一个错误。 TIA!

String1... String2... String3... String4... etc

我尝试了以下方法,但似乎并不像预期的那样

if grep -v 'excel' document.txt | grep -v 'word'; then
  echo "error"
fi

您可以使用单个模式匹配单词或 excel 或使用交替的空行并检查是否存在相反的结果,并使用 --quiet 不写入标准输出。

if grep --quiet -v 'word\|excel\|^[[:space:]]*$' document.txt; then
  echo "error"
fi