How can we use grep
to get line(s) containing a group of words, while the order of words is not important, although line(s) should contain all words mentioned in search?
I have done that with phasing the search (with piping, saving the outputs in temporary files, and searching again), but I'd like to know if I can do that in one attempt.
Here is a sample; I want to search in the lines below for lines containing sample, words, list:
it's a sample test file for a list of words.
list of words without a specific order, it's a sample.
line with no search keyword here.
list the sample files.
something completely different.
And get this result:
it's a sample test file for a list of words.
list of words without a specific order, it's a sample.
The easy way is with multiple calls to
grep
:A demonstration:
You can use lookaheads with Perl-regex (
-P
):Example:
(via)
With
agrep
(sudo apt install agrep
) you can chain multiple patterns:(via)