I wish to pass each line of a file as a grep pattern to be searched in data.txt file.

Thus, i need cat allsearchstrings.txt | grep <should be lines of allsearchstrings.txt> data.txt

cat allsearchstring

: 'CANV'
: 'MY'

I think xargs could be the solution but I'm not sure how-to.

Note: the content of allsearchstrings.txt may have single quotes '

Could you please suggest?

  • 1
    Are you sure you want to do that? grep should be able to read patterns from a file directly (with the -f option) Apr 25, 2022 at 0:26
  • 1
    It not a file but output of some complex command which i wanted to avoid sharing.
    – Ashar
    Apr 25, 2022 at 0:40
  • 1
    ... in that case you can try complex_command | grep -F -f - data.txt or grep -F -f <(complex_command) data.txt if your shell supports process substitution Apr 25, 2022 at 0:57
  • 2
    If it's not a file but command output, then in the future, be clear about what it is instead of stating I wish to pass each line of a file as a grep pattern and using cat allsearchstrings.txt as an example. That doesn't line up with what's actually going on and makes it more difficult for people to answer your question. Apr 25, 2022 at 1:27
  • 1
    @steeldriver yes, it answers my question
    – Ashar
    Apr 25, 2022 at 1:39


Browse other questions tagged .