Find the answer to your Linux question:
Results 1 to 9 of 9
Hello, Is there a way using /bin/grep that I print lines that contains BOTH String1 and String2? Thanks....
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    9

    grep question

    Hello,
    Is there a way using /bin/grep that I print lines that contains BOTH String1 and String2?

    Thanks.

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    Code:
    grep <STRING1> filename | grep <STRING2>

  3. #3
    Just Joined!
    Join Date
    Mar 2009
    Posts
    9
    Thanks. I was dabbling with Regular expressions ... I guess my next question is how do I match a line which contains EITHER String1 or String2

    Thanks again.

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    C'mon man...

    Just run two commands...
    Code:
    grep <STRING2> filename
    Code:
    grep <STRING1> filename

  5. #5
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    Or you can use one command:

    Code:
    grep -e 'string1' -e 'string2' filename

  6. #6
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    Quote Originally Posted by lomcevak View Post
    Or you can use one command:

    Code:
    grep -e 'string1' -e 'string2' filename
    Absolutely true, but the "no brainer" way to me would be to run two commands - no thought required.

    Maybe there is some weird "one command" requirement. But if so, it would be helpful if that was included in the OP's question.

    Does this count?

    Code:
    grep <STRING1> filename; grep <STRING2> filename

  7. #7
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    Quote Originally Posted by HROAdmin26 View Post
    Does this count?

    Code:
    grep <STRING1> filename; grep <STRING2> filename
    Well it does save you from having to press <Enter> twice.

    The OP said he/she was dabbling in regex. So, you also can do it with extended regex as follows:

    Code:
    grep -E '(string1|string2)' filename

  8. #8
    Linux Newbie SagaciousKJB's Avatar
    Join Date
    Aug 2007
    Location
    Yakima, WA
    Posts
    162
    Quote Originally Posted by HROAdmin26 View Post
    Code:
    grep <STRING1> filename | grep <STRING2>
    This will pipe only lines which contain string1 to grep, which will try to find lines that containe string2, but will not find any because the first grep command only found string1.

    Code:
    sagacious@amd:~$ cat ./testfile
    string1
    string2
    
    sagacious@amd:~$ grep string1 ./testfile | grep string2
    sagacious@amd:~$
    Your other example specifies the filename, which doesn't account for using STDIN, but isn't really what the OP wanted.

    The OP wanted grep to match lines that contained string1 and string2 in the same execution. I suppose that's obvious now, but I guess I felt it necessary to correct your first example.

    I personally use the regex pattern that lomcevak posted.

  9. #9
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    Quote Originally Posted by SagaciousKJB View Post
    This will pipe only lines which contain string1 to grep, which will try to find lines that containe string2, but will not find any because the first grep command only found string1.

    Code:
    sagacious@amd:~$ cat ./testfile
    string1
    string2
    
    sagacious@amd:~$ grep string1 ./testfile | grep string2
    sagacious@amd:~$
    Your other example specifies the filename, which doesn't account for using STDIN, but isn't really what the OP wanted.

    The OP wanted grep to match lines that contained string1 and string2 in the same execution. I suppose that's obvious now, but I guess I felt it necessary to correct your first example.

    I personally use the regex pattern that lomcevak posted.
    Please check your sample file again. You have string1 and string2 in TWO lines.

    Of course you will not find string1 and string2 in the same line using your test. Try making a valid test file (or use the messages log) and you will see it works fine.

    The OP wanted grep to match lines that contained string1 and string2 in the same execution.
    No, that is *your* assumption and OP never made any such requirement.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...