Regex 如何使用GREP检查文件中是否存在两个单词

Regex 如何使用GREP检查文件中是否存在两个单词,regex,shell,scripting,grep,Regex,Shell,Scripting,Grep,我有两个文件A.txt和B.txt,分别包含两个列表,如下所示 文件A.txt hello hi ko 文件B.txt fine No And how why 现在,我想检查在另一个文件C.txt的一行中是否存在这些单词(来自A.txt和B.txt) 我正在使用grep命令 grep -iof A.txt C.txt| grep B.txt C.txt包含包含来自A.txt和B.txt的单词的句子 Hello I am fine I am not fine why ko is and

我有两个文件A.txt和B.txt,分别包含两个列表,如下所示

文件A.txt

hello
hi 
ko
文件B.txt

fine
No 
And how
why
现在,我想检查在另一个文件C.txt的一行中是否存在这些单词(来自A.txt和B.txt)

我正在使用grep命令

grep -iof A.txt C.txt| grep B.txt
C.txt包含包含来自A.txt和B.txt的单词的句子

Hello I am fine
I am not fine
why ko is and how?
不显示任何输出

所以,现在我想,如果A.txt和B.txt中的任何单词同时出现在一个句子中,它应该显示输出为

Hello fine
why ko and how 

如果两个文件中的匹配字同时出现在C.txt中,则只打印它们,而不是从C.txt打印整行,您可能想说:

$ grep -if B <(grep -if A C)
Hello I am fine
why ko is and how?
然后,将其输出与
B
中的内容进行比较

$ grep -if B <(grep -if A C)
Hello I am fine        # "fine" highlighted
why ko is and how?     # "and how" highlighted

你说这两个词中的任何一个都存在。我不确定在这种情况下我是否理解。你的意思是这些词的出现,是吗?也就是说,您想知道C.txt中是否有一行,其中包含a.txt或B.txt中的单词。对吗?我刚刚编辑了这个问题。我的意思是在C.txt中同时出现A.txt和B.txt中的任何一个词。我不太理解你在本例中同时出现的意思,但既然你已经接受了答案,我想这就是你想要的。;-)我在哪里提到这两个文件?为了安全起见,我认为应该是
fgrep
,而不是
grep
,以防其中一个模式文件包含句点或$等字符。另一个问题是OP想要匹配单词,而不是字符串。Grepping,即“hi”,也将匹配例如“hint”,这可能不是OP想要的。在任何情况下,我认为在我们提出解决方案之前,应该更准确地说明这个问题。我刚刚编辑了,主要目的是找出C中同时出现的a.txt和B.txt中的任何单词。txt@Doej请参阅我的更新,
$grep-fb@user1934428 good point。我添加(或建议)了
-F
-I
-w
$ grep -if B <(grep -if A C)
Hello I am fine        # "fine" highlighted
why ko is and how?     # "and how" highlighted
   -f FILE, --file=FILE
          Obtain  patterns  from  FILE,  one  per  line.   The  empty file
          contains zero patterns, and therefore matches nothing.   (-f  is
          specified by POSIX.)

   -F, --fixed-strings
          Interpret PATTERN as a  list  of  fixed  strings,  separated  by
          newlines,  any  of  which is to be matched.  (-F is specified by
          POSIX.)

   -i, --ignore-case
          Ignore  case  distinctions  in  both  the  PATTERN and the input
          files.  (-i is specified by POSIX.)

   -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.