假设我有两个文件 ( file1.txt
& file2.txt
)。
file1.txt
:(仅包含单词列表,每行一个)
Car
Ricky
file2.txt
:(包含使用以下单词的行(短语)file1.txt
)
he has a Car
there is no food
I have a book
road is straight
Ricky is a good student
输出应该是:
he has a Car
Ricky is a good student
答案1
如果您grep
支持该-w
选项:
grep -wFf file1.txt file2.txt
答案2
如果我正确理解了这个问题,听起来您想从 file2.txt 中获取包含 file1.txt 中的单词的行
grep
这可以通过使用and循环轻松实现for
。
基本上,您可以 cat file1.txt 然后将其输入 grep 命令。
for i in $(cat file1.txt); do grep $i file2.txt; done