I have a text file containing names (nameslist.txt
) and I want to read them using cat
and pipe the result with xargs
to a grep
command, so that grep
checks the existence of each name that it receives in a target file (targetfile.txt
).
Let's say targetfile.txt
contains a large number of names which might have some of those names in nameslist.txt
.
What should I add between xargs
and grep
, and between grep
and ./targetfile.txt
, below?
cat ./nameslist.txt | xargs grep ./targetfile.txt
Thank you
You can use
-I
to tellxargs
to use a particular character or sequence of characters as a placeholder for the argument. Fromman xargs
:A common choice is
{}
soor (without the useless use of cat)
However, assuming your list has one name per line you don't need
xargs
here at all -grep
can read a list of patterns (or fixed strings, with the-F
option) from a file: