How can I find all lines containing two specified words?

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











up vote
10
down vote

favorite












I need to check if two (specified) words 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 file that contain the two words “cat” and “elephant” together (i.e., on the same line;
not necessarily side-by-side):



Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environments
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 environments


I tried grep and awk with no hope. The problem is there are words that have upper and lower case, so how can I match for both words regardless of their 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.















  • 2




    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
    19 hours ago






  • 2




    Related: grep with logic operators
    – steeldriver
    17 hours ago










  • How to run grep with multiple AND patterns?, How to use grep to match multiple strings in the same line?, grep for 2 words existing on the same line, Grep searching two words in a line
    – phuclv
    3 hours ago






  • 3




    Possible duplicate of How to run grep with multiple AND patterns?
    – phuclv
    3 hours ago










  • or Match two words that are on the same line
    – don_crissti
    7 mins ago














up vote
10
down vote

favorite












I need to check if two (specified) words 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 file that contain the two words “cat” and “elephant” together (i.e., on the same line;
not necessarily side-by-side):



Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environments
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 environments


I tried grep and awk with no hope. The problem is there are words that have upper and lower case, so how can I match for both words regardless of their 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.















  • 2




    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
    19 hours ago






  • 2




    Related: grep with logic operators
    – steeldriver
    17 hours ago










  • How to run grep with multiple AND patterns?, How to use grep to match multiple strings in the same line?, grep for 2 words existing on the same line, Grep searching two words in a line
    – phuclv
    3 hours ago






  • 3




    Possible duplicate of How to run grep with multiple AND patterns?
    – phuclv
    3 hours ago










  • or Match two words that are on the same line
    – don_crissti
    7 mins ago












up vote
10
down vote

favorite









up vote
10
down vote

favorite











I need to check if two (specified) words 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 file that contain the two words “cat” and “elephant” together (i.e., on the same line;
not necessarily side-by-side):



Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environments
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 environments


I tried grep and awk with no hope. The problem is there are words that have upper and lower case, so how can I match for both words regardless of their 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 (specified) words 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 file that contain the two words “cat” and “elephant” together (i.e., on the same line;
not necessarily side-by-side):



Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environments
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 environments


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







text-processing awk grep






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 18 mins ago









G-Man

12k92860




12k92860






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 19 hours ago









gormet

565




565




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.







  • 2




    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
    19 hours ago






  • 2




    Related: grep with logic operators
    – steeldriver
    17 hours ago










  • How to run grep with multiple AND patterns?, How to use grep to match multiple strings in the same line?, grep for 2 words existing on the same line, Grep searching two words in a line
    – phuclv
    3 hours ago






  • 3




    Possible duplicate of How to run grep with multiple AND patterns?
    – phuclv
    3 hours ago










  • or Match two words that are on the same line
    – don_crissti
    7 mins ago












  • 2




    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
    19 hours ago






  • 2




    Related: grep with logic operators
    – steeldriver
    17 hours ago










  • How to run grep with multiple AND patterns?, How to use grep to match multiple strings in the same line?, grep for 2 words existing on the same line, Grep searching two words in a line
    – phuclv
    3 hours ago






  • 3




    Possible duplicate of How to run grep with multiple AND patterns?
    – phuclv
    3 hours ago










  • or Match two words that are on the same line
    – don_crissti
    7 mins ago







2




2




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
19 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
19 hours ago




2




2




Related: grep with logic operators
– steeldriver
17 hours ago




Related: grep with logic operators
– steeldriver
17 hours ago












How to run grep with multiple AND patterns?, How to use grep to match multiple strings in the same line?, grep for 2 words existing on the same line, Grep searching two words in a line
– phuclv
3 hours ago




How to run grep with multiple AND patterns?, How to use grep to match multiple strings in the same line?, grep for 2 words existing on the same line, Grep searching two words in a line
– phuclv
3 hours ago




3




3




Possible duplicate of How to run grep with multiple AND patterns?
– phuclv
3 hours ago




Possible duplicate of How to run grep with multiple AND patterns?
– phuclv
3 hours ago












or Match two words that are on the same line
– don_crissti
7 mins ago




or Match two words that are on the same line
– don_crissti
7 mins ago










4 Answers
4






active

oldest

votes

















up vote
12
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


@glenn jackman suggested that 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
    15 hours 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
    15 hours ago







  • 1




    @glenn: IGNORECASE is indeed GNU; tolower($0)~/cat/ (or similarly with toupper) is standard, but may give undesired results in some (non-English) locales with accented letters and especially Turkish with its dotted and dotless i's.
    – dave_thompson_085
    12 hours ago

















up vote
10
down vote













$ grep -Fiw cat <file | grep -Fiw elephant
Cat is smaller than elephant
Elephant is larger than cat


We first extract all lines from the file file that contains the word cat and then narrow down those lines to the ones that contains the word elephant.



This is done using grep -F -i -w where




  • -F makes grep treat the pattern as a fixed string, not as a regular expression,


  • -i makes grep do case-insensitive matching, and


  • -w makes grep match complete words only.

The -w option is an extension of the POSIX standard for grep, but is implemented by most common grep implementations. It basically disallows matches of the given patten when the matching string is part of a longer word.



Note that I'm not matching the line



Cat and elephants live in different environment


This is due to the final s in elephants. I would also not match the line



elephantiasis is catastrophic


for the same reason.



Would you want to allow for a plural s at the end of words, use



$ grep -Eiw 'cats?' <file | grep -Eiw 'elephants?'
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment


Here, we use an (extended) regular expression instead of a fixed string in both invocations of grep. The expressions will match an optional s at the end of the two words. Now we match cat and cats (case-insensitively), but would not match catnip, catsup, or scat.






share|improve this answer





























    up vote
    8
    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
      2
      down vote













      You can do it in non-GNU awk
      by using the “poor man’s” trick to get case insensitivity:


      awk '/[Cc][Aa][Tt]/ && /[Ee][Ll][Ee][Pp][Hh][Aa][Nn][Tt]/' file

      where, just as [aeiou] matches any one of a, e, i, o or u, 
      [Ee] matches either E or e — that is, a case-insensitive match for “e”.

      Note that this approach (like all the other answers posted here so far)
      will match the line


      There are many ways to catch an elephant.

      because the word “catch” contains the string “cat”. 
      If you want to avoid this, try
      awk '/(^|W)[Cc][Aa]Tt/ && /(^|W)[Ee][Ll][Ee][Pp][Hh][Aa][Nn]Tt/' file
      where you constrain each word
      to be preceded by a non-word character (or the beginning of the line)
      and followed by a non-word character (or the end of the line) —
      W matches a non-word character (i.e., a space (or tab)
      or other non-alphanumeric * character).

      (I’m not sure whether this is POSIX-compliant.)



      Note that this will now not match


      Cat and elephants live in different environments
      because the word “elephants” is not the same as the word “elephant”.

      __________________
      * In this context, underscore (the “_” character) counts as a letter.




      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-specified-words%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        12
        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


        @glenn jackman suggested that 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
          15 hours 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
          15 hours ago







        • 1




          @glenn: IGNORECASE is indeed GNU; tolower($0)~/cat/ (or similarly with toupper) is standard, but may give undesired results in some (non-English) locales with accented letters and especially Turkish with its dotted and dotless i's.
          – dave_thompson_085
          12 hours ago














        up vote
        12
        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


        @glenn jackman suggested that 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
          15 hours 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
          15 hours ago







        • 1




          @glenn: IGNORECASE is indeed GNU; tolower($0)~/cat/ (or similarly with toupper) is standard, but may give undesired results in some (non-English) locales with accented letters and especially Turkish with its dotted and dotless i's.
          – dave_thompson_085
          12 hours ago












        up vote
        12
        down vote



        accepted







        up vote
        12
        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


        @glenn jackman suggested that 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


        @glenn jackman suggested that 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 15 hours ago

























        answered 19 hours ago









        Goro

        10k64892




        10k64892











        • The print $0 block is optional since it is the default action.
          – glenn jackman
          15 hours 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
          15 hours ago







        • 1




          @glenn: IGNORECASE is indeed GNU; tolower($0)~/cat/ (or similarly with toupper) is standard, but may give undesired results in some (non-English) locales with accented letters and especially Turkish with its dotted and dotless i's.
          – dave_thompson_085
          12 hours ago
















        • The print $0 block is optional since it is the default action.
          – glenn jackman
          15 hours 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
          15 hours ago







        • 1




          @glenn: IGNORECASE is indeed GNU; tolower($0)~/cat/ (or similarly with toupper) is standard, but may give undesired results in some (non-English) locales with accented letters and especially Turkish with its dotted and dotless i's.
          – dave_thompson_085
          12 hours ago















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





        The print $0 block is optional since it is the default action.
        – glenn jackman
        15 hours 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
        15 hours 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
        15 hours ago





        1




        1




        @glenn: IGNORECASE is indeed GNU; tolower($0)~/cat/ (or similarly with toupper) is standard, but may give undesired results in some (non-English) locales with accented letters and especially Turkish with its dotted and dotless i's.
        – dave_thompson_085
        12 hours ago




        @glenn: IGNORECASE is indeed GNU; tolower($0)~/cat/ (or similarly with toupper) is standard, but may give undesired results in some (non-English) locales with accented letters and especially Turkish with its dotted and dotless i's.
        – dave_thompson_085
        12 hours ago












        up vote
        10
        down vote













        $ grep -Fiw cat <file | grep -Fiw elephant
        Cat is smaller than elephant
        Elephant is larger than cat


        We first extract all lines from the file file that contains the word cat and then narrow down those lines to the ones that contains the word elephant.



        This is done using grep -F -i -w where




        • -F makes grep treat the pattern as a fixed string, not as a regular expression,


        • -i makes grep do case-insensitive matching, and


        • -w makes grep match complete words only.

        The -w option is an extension of the POSIX standard for grep, but is implemented by most common grep implementations. It basically disallows matches of the given patten when the matching string is part of a longer word.



        Note that I'm not matching the line



        Cat and elephants live in different environment


        This is due to the final s in elephants. I would also not match the line



        elephantiasis is catastrophic


        for the same reason.



        Would you want to allow for a plural s at the end of words, use



        $ grep -Eiw 'cats?' <file | grep -Eiw 'elephants?'
        Cat is smaller than elephant
        Elephant is larger than cat
        Cat and elephants live in different environment


        Here, we use an (extended) regular expression instead of a fixed string in both invocations of grep. The expressions will match an optional s at the end of the two words. Now we match cat and cats (case-insensitively), but would not match catnip, catsup, or scat.






        share|improve this answer


























          up vote
          10
          down vote













          $ grep -Fiw cat <file | grep -Fiw elephant
          Cat is smaller than elephant
          Elephant is larger than cat


          We first extract all lines from the file file that contains the word cat and then narrow down those lines to the ones that contains the word elephant.



          This is done using grep -F -i -w where




          • -F makes grep treat the pattern as a fixed string, not as a regular expression,


          • -i makes grep do case-insensitive matching, and


          • -w makes grep match complete words only.

          The -w option is an extension of the POSIX standard for grep, but is implemented by most common grep implementations. It basically disallows matches of the given patten when the matching string is part of a longer word.



          Note that I'm not matching the line



          Cat and elephants live in different environment


          This is due to the final s in elephants. I would also not match the line



          elephantiasis is catastrophic


          for the same reason.



          Would you want to allow for a plural s at the end of words, use



          $ grep -Eiw 'cats?' <file | grep -Eiw 'elephants?'
          Cat is smaller than elephant
          Elephant is larger than cat
          Cat and elephants live in different environment


          Here, we use an (extended) regular expression instead of a fixed string in both invocations of grep. The expressions will match an optional s at the end of the two words. Now we match cat and cats (case-insensitively), but would not match catnip, catsup, or scat.






          share|improve this answer
























            up vote
            10
            down vote










            up vote
            10
            down vote









            $ grep -Fiw cat <file | grep -Fiw elephant
            Cat is smaller than elephant
            Elephant is larger than cat


            We first extract all lines from the file file that contains the word cat and then narrow down those lines to the ones that contains the word elephant.



            This is done using grep -F -i -w where




            • -F makes grep treat the pattern as a fixed string, not as a regular expression,


            • -i makes grep do case-insensitive matching, and


            • -w makes grep match complete words only.

            The -w option is an extension of the POSIX standard for grep, but is implemented by most common grep implementations. It basically disallows matches of the given patten when the matching string is part of a longer word.



            Note that I'm not matching the line



            Cat and elephants live in different environment


            This is due to the final s in elephants. I would also not match the line



            elephantiasis is catastrophic


            for the same reason.



            Would you want to allow for a plural s at the end of words, use



            $ grep -Eiw 'cats?' <file | grep -Eiw 'elephants?'
            Cat is smaller than elephant
            Elephant is larger than cat
            Cat and elephants live in different environment


            Here, we use an (extended) regular expression instead of a fixed string in both invocations of grep. The expressions will match an optional s at the end of the two words. Now we match cat and cats (case-insensitively), but would not match catnip, catsup, or scat.






            share|improve this answer














            $ grep -Fiw cat <file | grep -Fiw elephant
            Cat is smaller than elephant
            Elephant is larger than cat


            We first extract all lines from the file file that contains the word cat and then narrow down those lines to the ones that contains the word elephant.



            This is done using grep -F -i -w where




            • -F makes grep treat the pattern as a fixed string, not as a regular expression,


            • -i makes grep do case-insensitive matching, and


            • -w makes grep match complete words only.

            The -w option is an extension of the POSIX standard for grep, but is implemented by most common grep implementations. It basically disallows matches of the given patten when the matching string is part of a longer word.



            Note that I'm not matching the line



            Cat and elephants live in different environment


            This is due to the final s in elephants. I would also not match the line



            elephantiasis is catastrophic


            for the same reason.



            Would you want to allow for a plural s at the end of words, use



            $ grep -Eiw 'cats?' <file | grep -Eiw 'elephants?'
            Cat is smaller than elephant
            Elephant is larger than cat
            Cat and elephants live in different environment


            Here, we use an (extended) regular expression instead of a fixed string in both invocations of grep. The expressions will match an optional s at the end of the two words. Now we match cat and cats (case-insensitively), but would not match catnip, catsup, or scat.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 11 hours ago

























            answered 11 hours ago









            Kusalananda

            110k15214338




            110k15214338




















                up vote
                8
                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
                  8
                  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
                    8
                    down vote










                    up vote
                    8
                    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 hours ago









                    glenn jackman

                    49.2k467106




                    49.2k467106




















                        up vote
                        2
                        down vote













                        You can do it in non-GNU awk
                        by using the “poor man’s” trick to get case insensitivity:


                        awk '/[Cc][Aa][Tt]/ && /[Ee][Ll][Ee][Pp][Hh][Aa][Nn][Tt]/' file

                        where, just as [aeiou] matches any one of a, e, i, o or u, 
                        [Ee] matches either E or e — that is, a case-insensitive match for “e”.

                        Note that this approach (like all the other answers posted here so far)
                        will match the line


                        There are many ways to catch an elephant.

                        because the word “catch” contains the string “cat”. 
                        If you want to avoid this, try
                        awk '/(^|W)[Cc][Aa]Tt/ && /(^|W)[Ee][Ll][Ee][Pp][Hh][Aa][Nn]Tt/' file
                        where you constrain each word
                        to be preceded by a non-word character (or the beginning of the line)
                        and followed by a non-word character (or the end of the line) —
                        W matches a non-word character (i.e., a space (or tab)
                        or other non-alphanumeric * character).

                        (I’m not sure whether this is POSIX-compliant.)



                        Note that this will now not match


                        Cat and elephants live in different environments
                        because the word “elephants” is not the same as the word “elephant”.

                        __________________
                        * In this context, underscore (the “_” character) counts as a letter.




                        share|improve this answer
























                          up vote
                          2
                          down vote













                          You can do it in non-GNU awk
                          by using the “poor man’s” trick to get case insensitivity:


                          awk '/[Cc][Aa][Tt]/ && /[Ee][Ll][Ee][Pp][Hh][Aa][Nn][Tt]/' file

                          where, just as [aeiou] matches any one of a, e, i, o or u, 
                          [Ee] matches either E or e — that is, a case-insensitive match for “e”.

                          Note that this approach (like all the other answers posted here so far)
                          will match the line


                          There are many ways to catch an elephant.

                          because the word “catch” contains the string “cat”. 
                          If you want to avoid this, try
                          awk '/(^|W)[Cc][Aa]Tt/ && /(^|W)[Ee][Ll][Ee][Pp][Hh][Aa][Nn]Tt/' file
                          where you constrain each word
                          to be preceded by a non-word character (or the beginning of the line)
                          and followed by a non-word character (or the end of the line) —
                          W matches a non-word character (i.e., a space (or tab)
                          or other non-alphanumeric * character).

                          (I’m not sure whether this is POSIX-compliant.)



                          Note that this will now not match


                          Cat and elephants live in different environments
                          because the word “elephants” is not the same as the word “elephant”.

                          __________________
                          * In this context, underscore (the “_” character) counts as a letter.




                          share|improve this answer






















                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            You can do it in non-GNU awk
                            by using the “poor man’s” trick to get case insensitivity:


                            awk '/[Cc][Aa][Tt]/ && /[Ee][Ll][Ee][Pp][Hh][Aa][Nn][Tt]/' file

                            where, just as [aeiou] matches any one of a, e, i, o or u, 
                            [Ee] matches either E or e — that is, a case-insensitive match for “e”.

                            Note that this approach (like all the other answers posted here so far)
                            will match the line


                            There are many ways to catch an elephant.

                            because the word “catch” contains the string “cat”. 
                            If you want to avoid this, try
                            awk '/(^|W)[Cc][Aa]Tt/ && /(^|W)[Ee][Ll][Ee][Pp][Hh][Aa][Nn]Tt/' file
                            where you constrain each word
                            to be preceded by a non-word character (or the beginning of the line)
                            and followed by a non-word character (or the end of the line) —
                            W matches a non-word character (i.e., a space (or tab)
                            or other non-alphanumeric * character).

                            (I’m not sure whether this is POSIX-compliant.)



                            Note that this will now not match


                            Cat and elephants live in different environments
                            because the word “elephants” is not the same as the word “elephant”.

                            __________________
                            * In this context, underscore (the “_” character) counts as a letter.




                            share|improve this answer












                            You can do it in non-GNU awk
                            by using the “poor man’s” trick to get case insensitivity:


                            awk '/[Cc][Aa][Tt]/ && /[Ee][Ll][Ee][Pp][Hh][Aa][Nn][Tt]/' file

                            where, just as [aeiou] matches any one of a, e, i, o or u, 
                            [Ee] matches either E or e — that is, a case-insensitive match for “e”.

                            Note that this approach (like all the other answers posted here so far)
                            will match the line


                            There are many ways to catch an elephant.

                            because the word “catch” contains the string “cat”. 
                            If you want to avoid this, try
                            awk '/(^|W)[Cc][Aa]Tt/ && /(^|W)[Ee][Ll][Ee][Pp][Hh][Aa][Nn]Tt/' file
                            where you constrain each word
                            to be preceded by a non-word character (or the beginning of the line)
                            and followed by a non-word character (or the end of the line) —
                            W matches a non-word character (i.e., a space (or tab)
                            or other non-alphanumeric * character).

                            (I’m not sure whether this is POSIX-compliant.)



                            Note that this will now not match


                            Cat and elephants live in different environments
                            because the word “elephants” is not the same as the word “elephant”.

                            __________________
                            * In this context, underscore (the “_” character) counts as a letter.





                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 12 hours ago









                            G-Man

                            12k92860




                            12k92860




















                                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-specified-words%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                Confectionery