Grep command series: Use the grep command to search for multiple words

Source: Internet
Author: User

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:

  1. 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:

  1. $ 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:

  1. $ grep-w'warning\|error\|critical'/var/log/messages

The egrep command can skip the preceding syntax format. The syntax format is as follows:

  1. $ egrep -w'warning|error|critical'/var/log/messages

I recommend that you add the-I (case-insensitive) and -- color options, as shown below:

  1. $ 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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.