Search Multiple Words / String Pattern Use grep command

How do I search for multiple lines or words with the fingering command? For example, I want to search for word1, word2, word3 etc. in the /pad/to/ file. How do I get Grep to look for a few words? How can I lubricate multiple models on a Linux, OS X, FreeBSD, or Unix-like system? What is the multi-word search under Linux?

The grep command supports a regular expression template. Use the following syntax to find multiple templates:


How do I lubricate multiple stencils?

The syntax is as follows:

  1. Use some quotation marks in the template: fingering ‘pattern*’. Folder1 Folder2.
  2. Then use extended regular expressions: for example pattern1|pattern2 *.py.
  3. Finally, try the old Unix shells/axes: grap -e pattern1 -e pattern2 *.pl

Two-word search in line

Here are all other functions for grep and egrep:
grep ‘word1|word2|word3’ /path/k/file
#### Find all text files ####
grep ‘word*’. *.txt
#### Find all Python files ‘wordA’ or ‘wordB’ ###
grep ‘wordA*’ ‘wordB’ *.py
grep -E ‘word1|word2’ *.doc
grep -e string1 -e string2 *.pl
egrep word1|word2 *…c
### Display all strings that do not match the specified template/words/string(s) ###
grep -v ‘bar|foo’ /dir1/dir2/file1
egrep -v ‘pattern1|pattern2’ /path/to/file


In this example, enter a search warning, error and critical words in the /var/log/messages log file:
$ grep ‘warning|error|error|critical’ /var/log/messages
To find only the words, add the option -w:
$ grep -w ‘warning|error|critical’ /var/log/messages
Use the egrep command and you can skip the above syntax to find three words:
$ grep -w ‘warning|error|critical’ /var/log/messages

Handle various models

Sometimes we have to grease different models with special characters like – or — For example, look for a pattern that starts with the words –ca or –no. So, if you try the following, you will get an error message on the screen, for example grep: unrecognizable option: –ca|- no. Let’s try an example:
$ –help | egrep ‘–ca|–no’
To instruct grep not to treat ‘–‘ as a template for a command line option prefix, do the following:
$ –help | egrep — ‘–ca|–no’
$ virt-sysprep –help | egrep — ‘–(truncate|run)’
All other options for grep or egrep must appear before the final –. For example, pass -w and -color as follows: –help | egrep -color -w — ‘–ca|—no’
Search Multiple Words / String Pattern Use grep command

How do I find multiple lines in the folders?

Let’s try some more examples with extra options passed to grep/grep:
$ grep -e ‘warning|error|critical’ /var/log/messages
I also recommend passing the -i (case insensitive) and -colour options like these:
$ egrep -wi -colour ‘warning|error|critical’ /var/log/messages
Basic examples :
Search Multiple Words / String Pattern Use grep command Fig.01 : egrep command Linux / Unix Multiple word search Demo output
When the grep, pipe (|) command is used, the metacharacter must be represented by a backslash and there is no such requirement for egrep.
To search all *.conf files in /etc/, type:
# egrep -wi – color ‘foo|bar’ /etc/*.conf
To search recursively (including subdirectories) in the list, enter:
# egrep -Rwi – color ‘foo|bar’ /etc/
Where options are as follows:

  • -R : Recursive search
  • -w : Only suitable for words.
  • -i : Ignore the differences between the cases. In other words, the game FOO, Foo, Foo and so on.
  • -Color: Circle the corresponding color on the terminal. For example, display the corresponding lines in color.
  • -v : Turn the direction of play to select an unmatched line. In other words: Find and show any rules that do not match our rules or words.

Lubrication of multiple lines with awk

For example, if you are already using an awk or sed command, there is no need to go to grep and submit grep data. We can process and combine multiple lines of awk or sed to store the processor loop as follows

awk command syntax

awk ‘/error|critical/failed/’ /var/log/httpd/error_log
## Case search with *gnu/awk* ##
$ awk ‘BEGIN{IGNORECASE=1} /error|critical/failed/’ /var/log/messages
$ awk ‘/word1.*word2/’ Input
$ awk ‘/myPattern1/ && /myPattern2/’ /path/to/file
## awk does not match, i.e. all lines except HTTP/2.0 protocols are displayed ##
$ awk ‘!/HTTP/2.0/’ /var/log/nginx/cyberciti.bizerror_log.

Command syntax for sed

sed -e ‘/error/b’ -e ‘/critcial/b’ -e d /var/log/apache/nixcraft.com_error_log
$ sed ‘/stringOne/! d ; /stringTwo/! d’ ~/backups/conf.txt
## sed negative (NOT) match. For example, show all hosts except cbz01-www ##
$ sed -n ‘/cbz01-www/!p’ /etc/hosts
As you can see, the grep-syntax is easy to read and implement. I also provided the awk and sed syntax for your shell scripts.


You have learned to use the grep command to find multiple words or lines at once. For more information, see our grep tutorials or read the grep man page by typing the next man command or you can read it online here:


Published: Vivek gears

The author is the creator of nixCraft and an experienced system administrator, DevOps engineer and Linux/Unix shell script trainer. Receive the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly by email.grep multiple strings and condition,how to grep multiple words in a single line in unix,zgrep multiple strings,grep search for multiple strings in same line,grep two words with space,find and grep command in linux with example,grep multiple patterns r,grep either of two words