Grep command series: Use the grep command to search for multiple words
How can we use the grep command to search for multiple strings or words? For example, if I want to search for word1, word2, word3, and other words in the/path/to/file, how can I run grep to search for these words?
The grep command supports regular expression matching. To use multi-word search, use the following syntax:
grep'word1\|word2\|word3'/path/to/file
In the following example, search for the words warning, error, and critical in a text log file named/var/log/messages, and enter:
$ grep'warning\|error\|critical'/var/log/messages
You can add the-w option parameter to match a word (that is, if the word is separated by spaces in the west:
$ grep-w'warning\|error\|critical'/var/log/messages
The egrep command can skip the preceding syntax format. The syntax format is as follows:
$ egrep -w'warning|error|critical'/var/log/messages
I recommend that you add the-I (case-insensitive) and -- color options, as shown below:
$ egrep -wi --color 'warning|error|critical'/var/log/messages
Output example:
Fig.01: Linux/Unix egrep Command Search Multiple Words Demo Output
Figure 1: Linux/Unix egrep command to find multiple word output examples
Grep uses concise and Regular Expressions
Regular Expression usage
Assertion with Zero Width of a regular expression
Linux Command-grep for file text operations
Grep Regular Expression
Regular Expressions and file formatting commands in Linux (awk/grep/sed)
Via: http://www.cyberciti.biz/faq/searching-multiple-words-string-using-grep/
Author: Vivek Gite Translator: runningwater Proofreader: wxy
This article was originally compiled by LCTT and launched with the honor of Linux in China
This article permanently updates the link address: