从文件中获取多个单词

时间:2013-02-06 11:26:59

标签: file bash shell grep

我有一个文件(约1000字)。

的1.txt:

XYZ

ABC

DEF

GHI

...

我还有另一个文件 2.txt ,其中包含一些数据,现在我想在文件 2.txt中的 1.txt 中grep这些单词。 我使用了以下逻辑,但是给出了错误。

name=$(cat 1.txt |tr '\n' '|')

grep -E -w ${name} 2.txt

2 个答案:

答案 0 :(得分:3)

Grep可以使用-f选项从文件中读取模式:

egrep -w -f 1.txt 2.txt 

答案 1 :(得分:0)

你需要引用并使用egrep

egrep -w "$(cat 1.txt |tr '\n'  '|')" 2.txt

但是-f的答案更好

相关问题