How do I use grep to search for lines containing all words in a list with no specific order in a text file? [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite













This question already has an answer here:



  • Grep searching two words in a line

    7 answers



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.






share|improve this question














marked as duplicate by αғsнιη, RoVo, Thomas, Fabby, Bruni Aug 24 at 12:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Are the sentences on separate lines, can OP confirm this?
    – George Udosen
    Aug 23 at 14:20






  • 1




    This grep -e words -e sample -e list newfile works as well
    – George Udosen
    Aug 23 at 14:24






  • 1




    Hi George. Yes, each sentence is on a separate line.
    – arash deilami
    Aug 23 at 14:27






  • 2




    Probably awk better fits here.
    – PerlDuck
    Aug 23 at 14:34






  • 3




    @PerlDuck I agree, much easier. awk '(/sample/ && /words/ && /list/)' samplefile
    – Terrance
    Aug 23 at 14:43















up vote
2
down vote

favorite













This question already has an answer here:



  • Grep searching two words in a line

    7 answers



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.






share|improve this question














marked as duplicate by αғsнιη, RoVo, Thomas, Fabby, Bruni Aug 24 at 12:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Are the sentences on separate lines, can OP confirm this?
    – George Udosen
    Aug 23 at 14:20






  • 1




    This grep -e words -e sample -e list newfile works as well
    – George Udosen
    Aug 23 at 14:24






  • 1




    Hi George. Yes, each sentence is on a separate line.
    – arash deilami
    Aug 23 at 14:27






  • 2




    Probably awk better fits here.
    – PerlDuck
    Aug 23 at 14:34






  • 3




    @PerlDuck I agree, much easier. awk '(/sample/ && /words/ && /list/)' samplefile
    – Terrance
    Aug 23 at 14:43













up vote
2
down vote

favorite









up vote
2
down vote

favorite












This question already has an answer here:



  • Grep searching two words in a line

    7 answers



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.






share|improve this question















This question already has an answer here:



  • Grep searching two words in a line

    7 answers



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.




This question already has an answer here:



  • Grep searching two words in a line

    7 answers









share|improve this question













share|improve this question




share|improve this question








edited Aug 26 at 14:06









Peter Mortensen

1,03821016




1,03821016










asked Aug 23 at 14:12









arash deilami

255




255




marked as duplicate by αғsнιη, RoVo, Thomas, Fabby, Bruni Aug 24 at 12:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by αғsнιη, RoVo, Thomas, Fabby, Bruni Aug 24 at 12:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • Are the sentences on separate lines, can OP confirm this?
    – George Udosen
    Aug 23 at 14:20






  • 1




    This grep -e words -e sample -e list newfile works as well
    – George Udosen
    Aug 23 at 14:24






  • 1




    Hi George. Yes, each sentence is on a separate line.
    – arash deilami
    Aug 23 at 14:27






  • 2




    Probably awk better fits here.
    – PerlDuck
    Aug 23 at 14:34






  • 3




    @PerlDuck I agree, much easier. awk '(/sample/ && /words/ && /list/)' samplefile
    – Terrance
    Aug 23 at 14:43

















  • Are the sentences on separate lines, can OP confirm this?
    – George Udosen
    Aug 23 at 14:20






  • 1




    This grep -e words -e sample -e list newfile works as well
    – George Udosen
    Aug 23 at 14:24






  • 1




    Hi George. Yes, each sentence is on a separate line.
    – arash deilami
    Aug 23 at 14:27






  • 2




    Probably awk better fits here.
    – PerlDuck
    Aug 23 at 14:34






  • 3




    @PerlDuck I agree, much easier. awk '(/sample/ && /words/ && /list/)' samplefile
    – Terrance
    Aug 23 at 14:43
















Are the sentences on separate lines, can OP confirm this?
– George Udosen
Aug 23 at 14:20




Are the sentences on separate lines, can OP confirm this?
– George Udosen
Aug 23 at 14:20




1




1




This grep -e words -e sample -e list newfile works as well
– George Udosen
Aug 23 at 14:24




This grep -e words -e sample -e list newfile works as well
– George Udosen
Aug 23 at 14:24




1




1




Hi George. Yes, each sentence is on a separate line.
– arash deilami
Aug 23 at 14:27




Hi George. Yes, each sentence is on a separate line.
– arash deilami
Aug 23 at 14:27




2




2




Probably awk better fits here.
– PerlDuck
Aug 23 at 14:34




Probably awk better fits here.
– PerlDuck
Aug 23 at 14:34




3




3




@PerlDuck I agree, much easier. awk '(/sample/ && /words/ && /list/)' samplefile
– Terrance
Aug 23 at 14:43





@PerlDuck I agree, much easier. awk '(/sample/ && /words/ && /list/)' samplefile
– Terrance
Aug 23 at 14:43











2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










The easy way is with multiple calls to grep:



grep sample testfile.txt | grep words | grep list


A demonstration:



echo -e "it's a sample test file for a list of words.nlist of words without a specific order, it's a sample. nline with no search keyword here. nlist the sample words. nsomething completely different." | grep sample | grep words | grep list
it's a sample test file for a list of words.
list of words without a specific order, it's a sample.
list the sample words.





share|improve this answer




















  • yes, this works as I've mentioned, but i'm looking for a one-line-solution. Although sometimes it's better to do things the easy way. thanks :)
    – arash deilami
    Aug 23 at 18:11










  • grep -e also selects lines which have only two words (not all the words). Maybe I should try awk instead of grep, it works fine. Thank you all
    – arash deilami
    Aug 23 at 18:18


















up vote
4
down vote













You can use lookaheads with Perl-regex (-P):



grep -P '(?=.*list)(?=.*words)(?=.*sample)'



Example:



echo "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 words.
sample line with only two of the keywords inside.
something completely different."
| grep -P '(?=.*list)(?=.*words)(?=.*sample)'

it's a sample test file for a list of words.
list of words without a specific order, it's a sample.
list the sample words.


(via)




With agrep (sudo apt install agrep) you can chain multiple patterns:



agrep "sample;words;list"


(via)






share|improve this answer





























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote



    accepted










    The easy way is with multiple calls to grep:



    grep sample testfile.txt | grep words | grep list


    A demonstration:



    echo -e "it's a sample test file for a list of words.nlist of words without a specific order, it's a sample. nline with no search keyword here. nlist the sample words. nsomething completely different." | grep sample | grep words | grep list
    it's a sample test file for a list of words.
    list of words without a specific order, it's a sample.
    list the sample words.





    share|improve this answer




















    • yes, this works as I've mentioned, but i'm looking for a one-line-solution. Although sometimes it's better to do things the easy way. thanks :)
      – arash deilami
      Aug 23 at 18:11










    • grep -e also selects lines which have only two words (not all the words). Maybe I should try awk instead of grep, it works fine. Thank you all
      – arash deilami
      Aug 23 at 18:18















    up vote
    4
    down vote



    accepted










    The easy way is with multiple calls to grep:



    grep sample testfile.txt | grep words | grep list


    A demonstration:



    echo -e "it's a sample test file for a list of words.nlist of words without a specific order, it's a sample. nline with no search keyword here. nlist the sample words. nsomething completely different." | grep sample | grep words | grep list
    it's a sample test file for a list of words.
    list of words without a specific order, it's a sample.
    list the sample words.





    share|improve this answer




















    • yes, this works as I've mentioned, but i'm looking for a one-line-solution. Although sometimes it's better to do things the easy way. thanks :)
      – arash deilami
      Aug 23 at 18:11










    • grep -e also selects lines which have only two words (not all the words). Maybe I should try awk instead of grep, it works fine. Thank you all
      – arash deilami
      Aug 23 at 18:18













    up vote
    4
    down vote



    accepted







    up vote
    4
    down vote



    accepted






    The easy way is with multiple calls to grep:



    grep sample testfile.txt | grep words | grep list


    A demonstration:



    echo -e "it's a sample test file for a list of words.nlist of words without a specific order, it's a sample. nline with no search keyword here. nlist the sample words. nsomething completely different." | grep sample | grep words | grep list
    it's a sample test file for a list of words.
    list of words without a specific order, it's a sample.
    list the sample words.





    share|improve this answer












    The easy way is with multiple calls to grep:



    grep sample testfile.txt | grep words | grep list


    A demonstration:



    echo -e "it's a sample test file for a list of words.nlist of words without a specific order, it's a sample. nline with no search keyword here. nlist the sample words. nsomething completely different." | grep sample | grep words | grep list
    it's a sample test file for a list of words.
    list of words without a specific order, it's a sample.
    list the sample words.






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Aug 23 at 14:20









    waltinator

    20.4k73968




    20.4k73968











    • yes, this works as I've mentioned, but i'm looking for a one-line-solution. Although sometimes it's better to do things the easy way. thanks :)
      – arash deilami
      Aug 23 at 18:11










    • grep -e also selects lines which have only two words (not all the words). Maybe I should try awk instead of grep, it works fine. Thank you all
      – arash deilami
      Aug 23 at 18:18

















    • yes, this works as I've mentioned, but i'm looking for a one-line-solution. Although sometimes it's better to do things the easy way. thanks :)
      – arash deilami
      Aug 23 at 18:11










    • grep -e also selects lines which have only two words (not all the words). Maybe I should try awk instead of grep, it works fine. Thank you all
      – arash deilami
      Aug 23 at 18:18
















    yes, this works as I've mentioned, but i'm looking for a one-line-solution. Although sometimes it's better to do things the easy way. thanks :)
    – arash deilami
    Aug 23 at 18:11




    yes, this works as I've mentioned, but i'm looking for a one-line-solution. Although sometimes it's better to do things the easy way. thanks :)
    – arash deilami
    Aug 23 at 18:11












    grep -e also selects lines which have only two words (not all the words). Maybe I should try awk instead of grep, it works fine. Thank you all
    – arash deilami
    Aug 23 at 18:18





    grep -e also selects lines which have only two words (not all the words). Maybe I should try awk instead of grep, it works fine. Thank you all
    – arash deilami
    Aug 23 at 18:18













    up vote
    4
    down vote













    You can use lookaheads with Perl-regex (-P):



    grep -P '(?=.*list)(?=.*words)(?=.*sample)'



    Example:



    echo "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 words.
    sample line with only two of the keywords inside.
    something completely different."
    | grep -P '(?=.*list)(?=.*words)(?=.*sample)'

    it's a sample test file for a list of words.
    list of words without a specific order, it's a sample.
    list the sample words.


    (via)




    With agrep (sudo apt install agrep) you can chain multiple patterns:



    agrep "sample;words;list"


    (via)






    share|improve this answer


























      up vote
      4
      down vote













      You can use lookaheads with Perl-regex (-P):



      grep -P '(?=.*list)(?=.*words)(?=.*sample)'



      Example:



      echo "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 words.
      sample line with only two of the keywords inside.
      something completely different."
      | grep -P '(?=.*list)(?=.*words)(?=.*sample)'

      it's a sample test file for a list of words.
      list of words without a specific order, it's a sample.
      list the sample words.


      (via)




      With agrep (sudo apt install agrep) you can chain multiple patterns:



      agrep "sample;words;list"


      (via)






      share|improve this answer
























        up vote
        4
        down vote










        up vote
        4
        down vote









        You can use lookaheads with Perl-regex (-P):



        grep -P '(?=.*list)(?=.*words)(?=.*sample)'



        Example:



        echo "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 words.
        sample line with only two of the keywords inside.
        something completely different."
        | grep -P '(?=.*list)(?=.*words)(?=.*sample)'

        it's a sample test file for a list of words.
        list of words without a specific order, it's a sample.
        list the sample words.


        (via)




        With agrep (sudo apt install agrep) you can chain multiple patterns:



        agrep "sample;words;list"


        (via)






        share|improve this answer














        You can use lookaheads with Perl-regex (-P):



        grep -P '(?=.*list)(?=.*words)(?=.*sample)'



        Example:



        echo "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 words.
        sample line with only two of the keywords inside.
        something completely different."
        | grep -P '(?=.*list)(?=.*words)(?=.*sample)'

        it's a sample test file for a list of words.
        list of words without a specific order, it's a sample.
        list the sample words.


        (via)




        With agrep (sudo apt install agrep) you can chain multiple patterns:



        agrep "sample;words;list"


        (via)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 23 at 14:51

























        answered Aug 23 at 14:25









        RoVo

        5,4671236




        5,4671236












            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            One-line joke