-1

Suppose I have two files named a.txt and b.txt. Is there any way to use all the contents of file a.txt to use as patterns for grep command if I want to search in file b.txt?

General format of grep is:

grep 'pattern' 'filename'
cuonglm
  • 150,973
  • 38
  • 327
  • 406
Black
  • 125
  • 1
  • 2
  • 12

2 Answers2

0

A full solution:

grep -f a.txt b.txt

Alternative style, applicable to a list of literal patterns (not regexps):

fgrep "`cat a.txt`" b.txt

Note that the fgrep command is equivalent to grep -F. The latter is a cheap, non-foolproof solution assuming the a.txt doesn’t start from the hyphen-minus character -.

Incnis Mrsi
  • 1,958
  • 18
  • 25
0

try

cat a.txt | grep -f - b.txt

or ( if you like <( ) )

grep -f <(cat a.txt) b.txt
Archemar
  • 31,183
  • 18
  • 69
  • 104