This question already has an answer here:

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'

marked as duplicate by cuonglm, Archemar, Costas, Ramesh, terdon Sep 13 '15 at 16:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Downvoted: homework detected. – Incnis Mrsi Sep 13 '15 at 16:19


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

or ( if you like <( ) )

grep -f <(cat a.txt) b.txt

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 -.

  • How I will use this as a pipeline with my command I sated in the post? @Incnis Mrsi – Black Sep 13 '15 at 15:57
  • @Black: Don’t understand where is “your command” specified. Is English your native tongue? – Incnis Mrsi Sep 13 '15 at 16:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.