The grep command in Linux, query multiple strings with or, regular expression base description

Source: Internet
Author: User
Tags perl regular expression egrep
using the grep ' word1|word2 ' filename is not the right command.


You should use the following command:

1,grep-e ' word1|word2 ' filename

2,egrep ' word1|word2 ' filename

3,grep ' word1/|word2 ' filename


Why do I need to add-e, about grep and Egrep:


Egrep equal to GREP-E. It interprets patterns in the pattern of extended regular expressions. The following help pages from grep:
Basic regular expression meta characters?, +, {, |, (and) have lost their original meaning, instead of using the backslash version/?,/+,/{,/|,/(and/). The traditional egrep does not support {metacharacters, some egrep implementations are/{substituted, so a portable script should avoid using {symbols in grep-e to match the literal {should use [}].
The GNU GREP-E attempts to support traditional usage, and it assumes that {is not a special character before the invalid interval specification string.
For example, the grep-e ' {1′ command searches for a string containing {12 characters without reporting a regular expression syntax error.

The POSIX.2 standard allows for an extension of this operation, but should be avoided in portable script files.


About the basic classification of regular Expressions:

1, the basic regular expression (fundamental Regular Expression also called basic RegEx abbreviation BREs)
2, extended Regular expression (Extended Regular Expression is also called Extended RegEx short Eres)
3, Perl Regular expression (Perl Regular Expression also known as Perl RegEx abbreviation PREs)

Some usages of basic regular expressions and extended regular expressions:

Basic Regular Expressions

Meta data

Meaning and Paradigm

^word

Searches for a line that starts with Word.

For example: Search for a script comment line that starts with a #

Grep–n ' ^# ' regular.txt

word$

Search for lines that end in Word

For example, search with '. ' End of Line

Grep–n '. $ ' regular.txt

.

matches any one character.

For example: Grep–n ' E.E ' regular.txt

Matches E and E have any one character, can match eee,eae,eve, but does not match EE.

\

The escape character.

For example: Search ', ' is a special character that has a special meaning in regular expressions. Must be escaped first.

Grep–n ' \ ' Regular.txt

*

The preceding characters are repeated 0 to many times.

such as matching gle,gogle,google,gooogle, etc.

Grep–n ' Go*gle ' regular.txt

[List]

Matches one of a series of characters.

For example: matching GL,GF.

Grep–n ' g[lf] ' regular.txt

[N1-N2]

Matches one character in a range of characters.

For example: matching numeric characters

Grep–n ' [0-9] ' regular.txt

[^list]

Match characters other than character sets

For example: Grep–n ' [^o] ' regular.txt

Matching non-O characters

\{n1,n2\}

The preceding character repeats n1,n2 times

For example: matching Google,gooogle.

Grep–n ' Go\{2,3\}gle ' regular.txt

\<word

The word is the beginning.

For example: Match a word that starts with G

Grep–n ' \<g ' regular.txt

Word\>

Match the end of a word

For example: Match a word ending with tion

Grep–n ' tion\> ' regular.txt


Extending Regular Expressions

grep generally supports basic regular expressions, and can extend regular expressions through parameter-e support, in addition GREP provides an extended command that is called Egrep to support extended regular expressions, which is equivalent to GREP-E. Although in general, the basic regular expression is sufficient. In special cases, complex extended expressions can simplify string matching.

The extended regular expression is based on the basic regular expression, adding some meta data.

Meta data

Meaning and Paradigm

+

Repeat the previous character 1 to several times.

For example: matching God,good,goood and so on strings.

Grep–ne Go+d ' Regular.txt

?

Match 0 or 1 times before the characters

For example, matching Gd,god

Grep–ne ' Go?d ' regular.txt

|

or (or) to match multiple strings
For example: Grep–ne ' God|good ' regular.txt

Match either God or good.

()

Matches the entire bracketed string, which turns out to match a single character

For example: Search for good or glad

Grep–ne ' g (oo|la) ' Regular.txt

()

The preceding characters are repeated 0 to many times.

such as matching gle,gogle,google,gooogle, etc.

Grep–ne ' Go*gle ' regular.txt


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.