[How To] Search multiple strings in text file

Search multiple strings in text file using grep / egrep

grep is normally used to search a text pattern / regular expression in a text file. However, grep also comes handy when you wish to search multiple regular expressions. You can list only those lines which contain either pattern1 or pattern2 or both.

Supposing you have the below text file…

$ cat Tao.txt
Even a brief glance at various translations of the work of Lao Tzu will illustrate how
such a 'hidden curriculum' surreptitiously imposes itself upon even the most honest of
men, thus creating a major problem for the reader. This is the case even for the reader
who merely hopes to see an accurate English rendering of the work, but the reader's
problems are compounded if he or she seeks a translation which presents a reasonably
accurate description of Taoism (Tao Chia), the 'system' of which the Tao Te Ching is a
major work. It must be said of the existing English translations, that most treat the
Tao Te Ching as a literary or poetic work, whilst many others treat it as a work of
mysticism, rather than a work of classical scholarship, which I believe it to be,
describing the key concepts of Taoist philosophy (tao china) expressed in a poetic
manner. My intention here has been to provide a translation suitable for those readers
wishing to discover something of that philosophy, as described in one of its major
works.

And you wish to search the strings 'problem' and 'translation' from this file. Using grep, you can accomplish this as below. You need to separate the two strings with a pipe (|) and give \ as an escape character before the pipe.

$ grep 'problem\|translation' Tao.txt

Even a brief glance at various translations of the work of Lao Tzu will illustrate how
men, thus creating a major problem for the reader. This is the case even for the reader
problems are compounded if he or she seeks a translation which presents a reasonably
major work. It must be said of the existing English translations, that most treat the
manner. My intention here has been to provide a translation suitable for those readers

Alternatively if you wish to skip the escape character, you can do so using the comapct version of egrep.

$ egrep 'problem|translation' Tao.txt

Hope this helps!!