regexshellkshgrep

Using fgrep to find multiple words (korn shell)


Say I have a text file with multiple lines, but I only want fgrep to list those lines which have certain words in the same line. So, for example, if I'm looking for the words "cat" and "dog", how would I supply that information to fgrep?

I understand for one argument it would simply be:

    fgrep cat text.txt

but I want to look for lines that contain "dog" as well as "cat" in the same line. How would I go about doing this?


Solution

  • This will work:

    fgrep cat text.txt | fgrep dog
    

    You can also use one regex with grep -E, something like:

    grep -E "cat.*?dog|dog.*?cat" text.txt
    

    But it is typically too much of brainpower to spend for simple task like that, and I choose first method instead.