Combining xargs with cat and grep

I want to take the output from a file and feed it in as arguments for a grep command on the same file that I want to execute against:

:~$ cat example
cat
dog
mouse
ant

:~$ cat example | xargs cat example | grep
Usage: grep [OPTION]... PATTERN [FILE]...
cat: cat: No such file or directory
cat: dog: No such file or directory
cat: mouse: No such file or directory
cat: antTry 'grep --help' for more information.
: No such file or directory

In other words, expected commands would be:

cat example | grep cat
cat example | grep dog
cat example | grep mouse
cat example | grep ant


Read more here: https://stackoverflow.com/questions/65708629/combining-xargs-with-cat-and-grep

Content Attribution

This content was originally published by superloopnetworks at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: