Here, you just need:
grep -f queries.txt myDocument.txt
This causes grep
to read the regular expressions from the file queries.txt
and then apply them to myDocument.txt
.
In the xargs
version, you were effectively writing:
grep -f myDocument.txt cat dog 123
If you absolutely must use xargs
, then you'll need to write:
xargs -I % grep -e % myDocument.txt < queries.txt
This avoids a UUOC — Useless Use of cat
– award by redirecting standard input from queries.txt
. It uses the -I %
option to specify where the replacement text should go in the command line. Using the -e
option means that if the pattern is, say --help
, you won't run into problems with (GNU) grep
treating that as an argument (and therefore printing its help message).