Grep 如何在bash中将整个子字符串与分隔符匹配

Grep 如何在bash中将整个子字符串与分隔符匹配,grep,Grep,我有一根这样的绳子 "ABCD EFGH IJKL MNOP" 我让用户输入与这些子字符串中的每一个子字符串整体匹配,而不是部分匹配。 我该怎么做 这就是我目前正在做的事情 printf "\n Enter user input:" read userinput INPUT=$userinput if echo "ABCD EFGH IJKL MNOP" | grep -q "$INPUT"; then echo "Matching..."; else echo "Invali

我有一根这样的绳子

"ABCD EFGH IJKL MNOP"
我让用户输入与这些子字符串中的每一个子字符串整体匹配,而不是部分匹配。 我该怎么做

这就是我目前正在做的事情

printf "\n Enter user input:"

read userinput

INPUT=$userinput

if echo "ABCD EFGH IJKL MNOP" | grep -q "$INPUT"; then
  echo "Matching...";
else

  echo "Invalid entry ";
fi
上述代码的问题在于,它将匹配部分子字符串,如
“ABC”


“GH
”等我不想要的。我只需要用户输入与用分隔符分隔的整个子字符串进行比较。

使用
-w
grep
中匹配整个单词

echo "ABCD EFGH IJKL MNOP" | grep -w "$INPUT";
示例

>>> INPUT=ABC
>>> echo "ABCD EFGH IJKL MNOP" | grep -w "$INPUT";
>>>
>>> INPUT=ABCD
>>> echo "ABCD EFGH IJKL MNOP" | grep -w "$INPUT";
ABCD EFGH IJKL MNOP
grep-w

类似链接:

   -w, --word-regexp
          Select  only  those  lines  containing  matches  that form whole
          words.  The test is that the matching substring must  either  be
          at  the  beginning  of  the  line,  or  preceded  by  a non-word
          constituent character.  Similarly, it must be either at the  end
          of  the  line  or  followed by a non-word constituent character.
          Word-constituent  characters  are  letters,  digits,   and   the
          underscore.