我有两个文件A.txt和B.txt分别包含两个列表,如下所示。
文件A.txt
RDFDataMgr
文件B.txt
hello
hi
ko
现在我想在另一个文件C.txt的一行中检查这些单词(来自A.txt AND B.txt)的存在。
我正在使用grep命令
fine
No
And how
why
C.txt包含包含来自A.txt和B.txt
的单词的句子grep -iof A.txt C.txt| grep B.txt
不显示任何输出
所以,现在我想如果A.txt和B.txt中的任何单词同时出现在一个句子中,它应该将输出显示为
Hello I am fine
I am not fine
why ko is and how?
如果它们同时出现在C.txt中,则只打印两个文件中匹配的单词,而不是从C.txt打印整行
答案 0 :(得分:3)
你可能想说:
$ grep -if B <(grep -if A C)
Hello I am fine
why ko is and how?
这使用-f
来提供表达式。它可以是文件...或您使用process substitution <( ... )
动态创建的文件。
首先,grep -if A C
匹配C
中所有A
中的所有单词:
$ grep -if A C
Hello I am fine # "Hello" highlighted
why ko is and how? # "ko" highlighted
然后,将其输出与B
中的内容进行比较。
$ grep -if B <(grep -if A C)
Hello I am fine # "fine" highlighted
why ko is and how? # "and how" highlighted
根据您的需要,您可能希望添加-F
,-w
和-i
。
来自man grep
:
-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.