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'
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'
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 -
.
try
cat a.txt | grep -f - b.txt
or ( if you like <( )
)
grep -f <(cat a.txt) b.txt