how can I find all lines containing two words?

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











up vote
2
down vote

favorite












I need to check if two words are exist on any line in a text file. there are no limits for the characters of the words For example:



I want to find lines of a text that contain the two words cat and elephant together:



Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environment
cats are friendly


In the previous examples, how can I find the lines containing both words



Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment


I tried grep and awk with no hope, the problem is there are words have upper and lower case so how can I match for both words regardless of its letter status!?










share|improve this question









New contributor




gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    Try with grep again, but use grep -i. This makes its matching disregard the case of the letters. Also, please show what you've tried so that other's make comment and give suggestions for improvement. For example, does your command distinguish that "catnip" is not the word "cat"?
    – Kusalananda
    3 hours ago










  • Related: grep with logic operators
    – steeldriver
    1 hour ago














up vote
2
down vote

favorite












I need to check if two words are exist on any line in a text file. there are no limits for the characters of the words For example:



I want to find lines of a text that contain the two words cat and elephant together:



Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environment
cats are friendly


In the previous examples, how can I find the lines containing both words



Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment


I tried grep and awk with no hope, the problem is there are words have upper and lower case so how can I match for both words regardless of its letter status!?










share|improve this question









New contributor




gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    Try with grep again, but use grep -i. This makes its matching disregard the case of the letters. Also, please show what you've tried so that other's make comment and give suggestions for improvement. For example, does your command distinguish that "catnip" is not the word "cat"?
    – Kusalananda
    3 hours ago










  • Related: grep with logic operators
    – steeldriver
    1 hour ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I need to check if two words are exist on any line in a text file. there are no limits for the characters of the words For example:



I want to find lines of a text that contain the two words cat and elephant together:



Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environment
cats are friendly


In the previous examples, how can I find the lines containing both words



Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment


I tried grep and awk with no hope, the problem is there are words have upper and lower case so how can I match for both words regardless of its letter status!?










share|improve this question









New contributor




gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I need to check if two words are exist on any line in a text file. there are no limits for the characters of the words For example:



I want to find lines of a text that contain the two words cat and elephant together:



Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environment
cats are friendly


In the previous examples, how can I find the lines containing both words



Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment


I tried grep and awk with no hope, the problem is there are words have upper and lower case so how can I match for both words regardless of its letter status!?







bash






share|improve this question









New contributor




gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 10 mins ago





















New contributor




gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









gormet

164




164




New contributor




gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1




    Try with grep again, but use grep -i. This makes its matching disregard the case of the letters. Also, please show what you've tried so that other's make comment and give suggestions for improvement. For example, does your command distinguish that "catnip" is not the word "cat"?
    – Kusalananda
    3 hours ago










  • Related: grep with logic operators
    – steeldriver
    1 hour ago












  • 1




    Try with grep again, but use grep -i. This makes its matching disregard the case of the letters. Also, please show what you've tried so that other's make comment and give suggestions for improvement. For example, does your command distinguish that "catnip" is not the word "cat"?
    – Kusalananda
    3 hours ago










  • Related: grep with logic operators
    – steeldriver
    1 hour ago







1




1




Try with grep again, but use grep -i. This makes its matching disregard the case of the letters. Also, please show what you've tried so that other's make comment and give suggestions for improvement. For example, does your command distinguish that "catnip" is not the word "cat"?
– Kusalananda
3 hours ago




Try with grep again, but use grep -i. This makes its matching disregard the case of the letters. Also, please show what you've tried so that other's make comment and give suggestions for improvement. For example, does your command distinguish that "catnip" is not the word "cat"?
– Kusalananda
3 hours ago












Related: grep with logic operators
– steeldriver
1 hour ago




Related: grep with logic operators
– steeldriver
1 hour ago










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










With grep



grep -i "cat" file | grep -i "elephant"

Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment


The flag in grep is to ignore case (upper/lower)



 -i, --ignore-case ignore case distinctions


or awk



awk 'BEGINIGNORECASE=1 /cat/&&/elephant/print $0' file


Also, awk statement can be run as follows:



awk '/cat/&&/elephant/' IGNORECASE=1 file





share|improve this answer






















  • The print $0 block is optional since it is the default action.
    – glenn jackman
    16 mins ago











  • @glennjackman thank you so much for the valuable comment. Would suggest adding a note or just removing it entirely?
    – Goro
    14 mins ago











  • I would leave your answer as it is. FYI the awk command can be "golfed" to awk '/cat/&&/elephant/' IGNORECASE=1 file -- also I believe IGNORECASE is specific to GNU awk
    – glenn jackman
    13 mins ago











  • Perfect! I will add both.... you keep teaching me new things many thanks! ;-)
    – Goro
    12 mins ago

















up vote
1
down vote













with GNU sed:



sed -n '/cat/I /elephant/I p' file


or perl



perl -ne 'print if /cat/i and /elephant/i' file


or a single grep



grep -i -e 'cat.*elephant' -e 'elephant.*cat' file





share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    gormet is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475908%2fhow-can-i-find-all-lines-containing-two-words%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    With grep



    grep -i "cat" file | grep -i "elephant"

    Cat is smaller than elephant
    Elephant is larger than cat
    Cat and elephants live in different environment


    The flag in grep is to ignore case (upper/lower)



     -i, --ignore-case ignore case distinctions


    or awk



    awk 'BEGINIGNORECASE=1 /cat/&&/elephant/print $0' file


    Also, awk statement can be run as follows:



    awk '/cat/&&/elephant/' IGNORECASE=1 file





    share|improve this answer






















    • The print $0 block is optional since it is the default action.
      – glenn jackman
      16 mins ago











    • @glennjackman thank you so much for the valuable comment. Would suggest adding a note or just removing it entirely?
      – Goro
      14 mins ago











    • I would leave your answer as it is. FYI the awk command can be "golfed" to awk '/cat/&&/elephant/' IGNORECASE=1 file -- also I believe IGNORECASE is specific to GNU awk
      – glenn jackman
      13 mins ago











    • Perfect! I will add both.... you keep teaching me new things many thanks! ;-)
      – Goro
      12 mins ago














    up vote
    3
    down vote



    accepted










    With grep



    grep -i "cat" file | grep -i "elephant"

    Cat is smaller than elephant
    Elephant is larger than cat
    Cat and elephants live in different environment


    The flag in grep is to ignore case (upper/lower)



     -i, --ignore-case ignore case distinctions


    or awk



    awk 'BEGINIGNORECASE=1 /cat/&&/elephant/print $0' file


    Also, awk statement can be run as follows:



    awk '/cat/&&/elephant/' IGNORECASE=1 file





    share|improve this answer






















    • The print $0 block is optional since it is the default action.
      – glenn jackman
      16 mins ago











    • @glennjackman thank you so much for the valuable comment. Would suggest adding a note or just removing it entirely?
      – Goro
      14 mins ago











    • I would leave your answer as it is. FYI the awk command can be "golfed" to awk '/cat/&&/elephant/' IGNORECASE=1 file -- also I believe IGNORECASE is specific to GNU awk
      – glenn jackman
      13 mins ago











    • Perfect! I will add both.... you keep teaching me new things many thanks! ;-)
      – Goro
      12 mins ago












    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    With grep



    grep -i "cat" file | grep -i "elephant"

    Cat is smaller than elephant
    Elephant is larger than cat
    Cat and elephants live in different environment


    The flag in grep is to ignore case (upper/lower)



     -i, --ignore-case ignore case distinctions


    or awk



    awk 'BEGINIGNORECASE=1 /cat/&&/elephant/print $0' file


    Also, awk statement can be run as follows:



    awk '/cat/&&/elephant/' IGNORECASE=1 file





    share|improve this answer














    With grep



    grep -i "cat" file | grep -i "elephant"

    Cat is smaller than elephant
    Elephant is larger than cat
    Cat and elephants live in different environment


    The flag in grep is to ignore case (upper/lower)



     -i, --ignore-case ignore case distinctions


    or awk



    awk 'BEGINIGNORECASE=1 /cat/&&/elephant/print $0' file


    Also, awk statement can be run as follows:



    awk '/cat/&&/elephant/' IGNORECASE=1 file






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 11 mins ago

























    answered 3 hours ago









    Goro

    9,86364689




    9,86364689











    • The print $0 block is optional since it is the default action.
      – glenn jackman
      16 mins ago











    • @glennjackman thank you so much for the valuable comment. Would suggest adding a note or just removing it entirely?
      – Goro
      14 mins ago











    • I would leave your answer as it is. FYI the awk command can be "golfed" to awk '/cat/&&/elephant/' IGNORECASE=1 file -- also I believe IGNORECASE is specific to GNU awk
      – glenn jackman
      13 mins ago











    • Perfect! I will add both.... you keep teaching me new things many thanks! ;-)
      – Goro
      12 mins ago
















    • The print $0 block is optional since it is the default action.
      – glenn jackman
      16 mins ago











    • @glennjackman thank you so much for the valuable comment. Would suggest adding a note or just removing it entirely?
      – Goro
      14 mins ago











    • I would leave your answer as it is. FYI the awk command can be "golfed" to awk '/cat/&&/elephant/' IGNORECASE=1 file -- also I believe IGNORECASE is specific to GNU awk
      – glenn jackman
      13 mins ago











    • Perfect! I will add both.... you keep teaching me new things many thanks! ;-)
      – Goro
      12 mins ago















    The print $0 block is optional since it is the default action.
    – glenn jackman
    16 mins ago





    The print $0 block is optional since it is the default action.
    – glenn jackman
    16 mins ago













    @glennjackman thank you so much for the valuable comment. Would suggest adding a note or just removing it entirely?
    – Goro
    14 mins ago





    @glennjackman thank you so much for the valuable comment. Would suggest adding a note or just removing it entirely?
    – Goro
    14 mins ago













    I would leave your answer as it is. FYI the awk command can be "golfed" to awk '/cat/&&/elephant/' IGNORECASE=1 file -- also I believe IGNORECASE is specific to GNU awk
    – glenn jackman
    13 mins ago





    I would leave your answer as it is. FYI the awk command can be "golfed" to awk '/cat/&&/elephant/' IGNORECASE=1 file -- also I believe IGNORECASE is specific to GNU awk
    – glenn jackman
    13 mins ago













    Perfect! I will add both.... you keep teaching me new things many thanks! ;-)
    – Goro
    12 mins ago




    Perfect! I will add both.... you keep teaching me new things many thanks! ;-)
    – Goro
    12 mins ago












    up vote
    1
    down vote













    with GNU sed:



    sed -n '/cat/I /elephant/I p' file


    or perl



    perl -ne 'print if /cat/i and /elephant/i' file


    or a single grep



    grep -i -e 'cat.*elephant' -e 'elephant.*cat' file





    share|improve this answer
























      up vote
      1
      down vote













      with GNU sed:



      sed -n '/cat/I /elephant/I p' file


      or perl



      perl -ne 'print if /cat/i and /elephant/i' file


      or a single grep



      grep -i -e 'cat.*elephant' -e 'elephant.*cat' file





      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        with GNU sed:



        sed -n '/cat/I /elephant/I p' file


        or perl



        perl -ne 'print if /cat/i and /elephant/i' file


        or a single grep



        grep -i -e 'cat.*elephant' -e 'elephant.*cat' file





        share|improve this answer












        with GNU sed:



        sed -n '/cat/I /elephant/I p' file


        or perl



        perl -ne 'print if /cat/i and /elephant/i' file


        or a single grep



        grep -i -e 'cat.*elephant' -e 'elephant.*cat' file






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 15 mins ago









        glenn jackman

        49.1k467106




        49.1k467106




















            gormet is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            gormet is a new contributor. Be nice, and check out our Code of Conduct.












            gormet is a new contributor. Be nice, and check out our Code of Conduct.











            gormet is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475908%2fhow-can-i-find-all-lines-containing-two-words%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            Installing NextGIS Connect into QGIS 3?

            One-line joke