Got more questions? Find advice on: ASP | SQL | XML | Windows
Welcome to RegexAdvice Sign in | Join | Help

Search for two (or more) strings in a text file with one pattern (using grep)

  •  11-03-2006, 5:47 PM

    Search for two (or more) strings in a text file with one pattern (using grep)

    Okay, say I have this text file:
    bash-2.03$ cat test.txt
    The quick brown fox
    The silly black fox
    The code sequence is alpha foxtrot hotel tango.
    The elephant smokes pot.

    I want to be able to extract lines containing the words "The" and "foxtrot". I can achieve this easily by piping the file into grep, searching for one pattern, then piping those results into another grep process to search for the second pattern. Examples shown here:

    bash-2.03$ cat test.txt | grep The | grep foxtrot
    The code sequence is alpha foxtrot hotel tango.

    bash-2.03$ cat test.txt | grep The | grep fox
    The quick brown fox
    The silly black fox
    The code sequence is alpha foxtrot hotel tango.

    What I really want to accomplish is one regex pattern that will do the same thing with one grep process. I figure this should be either exceedingly easy or impossible.

View Complete Thread