Linux command grep, egrep, regular expression Daquan

Source: Internet
Author: User
Tags alphabetic character expression engine egrep

The Grep,egrep command in a Linux system is a powerful text-search tool that uses regular expressions to search for text and print matching lines. The grep full name is global Regular expression Print, which represents the globally regular expression version, and its use rights are for all users.

1.grep: The oldest text-matching program that uses the POSIX-defined basic regular expression (BRE) to match text.

2.egrep: Extended grep, which uses an extended formal expression (ERE) to match text.

3.fgrep: Fast grep, this version matches a fixed string rather than a regular expression. And is the only version that can match multiple strings in parallel.



grep syntax Format:


grep [option] ... ' PATTERN ' FILE ...




Options

-E: Use an extended regular expression to match, grep-e, or replace the Egrep command.

-F: Use a fixed string to match, grep-f or replace the traditional fgrep command.


-A #: Displays all subsequent lines that match the string.

-B #: Displays all preceding lines that match the string.

-C #: Displays the lines following the preceding line of the matching string.

-e: Typically the first non-option parameter is considered a pattern to match, or multiple schemas can be provided at the same time, as long as they are placed in single quotation marks and separated by newline characters.

When the pattern starts with a minus sign, the-e option indicates that the parameter after it is a pattern, even if he starts with a minus sign, to prevent confusion of it as an option.

-F: From the Pat-file file read mode as a match.

-I: Ignore case differences when pattern matching.

-L: Lists the file name of the matching pattern instead of printing the matching rows.

-Q: Silently, if the match succeeds, the matching row is not output to standard output; otherwise it is unsuccessful.

-S: Error message is not displayed, usually with-Q.

-V: Take the inverse.

-O: Displays only the matched string, not the line where the string is located.

--color=auto: The matching character appears in color.

-N: Displays matching lines and line numbers.

-C outputs only the count of matching rows.

Description: You can find content in multiple files at the same time, when you specify multiple files, each displayed file line will have a filename plus a colon to identify which file it came from.

You can use multiple-e or-f options to create a list of patterns to find.



Regular Expressions:

Related information: Http://zh.wikipedia.org/wiki/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F

is the pattern written by a class of characters, many of which do not represent their literal meaning, but rather the functions of expression control or wildcard;



Metacharacters

does not represent its literal meaning, but is used for additional functional descriptions



Regular Expressions:

Regular expression engine



Basic Regular expression: character grep with no special meaning

Extended regular Expressions: meta-characters, which have special meanings in regular expressions Egrep,grep-e

Fgrep:fast, using regular expressions is not supported




Character Matching:

.: Matches any single character

[]: matches any single character within the specified range

[0-9], [[:d Igit:]]: Numeric characters

[A-z], [[: Lower:]]: Lowercase alphabetic characters

[A-z], [[: Upper:]]: Uppercase characters

[[: Space:]]: space character

[[:p UNCT:]]: punctuation characters

[[: Alpha:]]: Alphabetic character

[[: Alnum:]]: Numeric characters




Number of matches metacharacters: the number of occurrences of the character that is specified before it

*: Any length, the characters in front of it can appear any time

Example: A*b

AAB, ABB, B,

\?: 0 or 1 times, the characters in front of it are optional

Example: A\?b

AB, B, CB

\{m\}: M times, it before the character to appear m times

Example: A\{2\}b

AB, AAB, B, Aaaab, ABB

\{m,n\}: At least m times, up to N times

Example: A\{2,5\}b

AB, B, AaB

\{m,\}: At least m times

\{0,n\}: Up to n times


Location anchoring:

^: Anchor at the beginning of the line; matches the regular expression immediately thereafter, the Bre only has a special meaning at the beginning of the regular expression, and Ere has special meanings in any position

Write on the left side of the pattern

$: End-of-line anchoring: matches the preceding regular expression, at the end of a string or line. The BRE has a special meaning only at the end of the regular expression, and Ere has a special meaning in any position

Write on the right side of the pattern

^$: Matching Blank lines

\: Typically used to turn on or off special meanings of subsequent characters, such as \ (... \) with \{...\}

[]: Matches any one of the characters in the square brackets, where the hyphen (-) is used to refer to the range of consecutive characters; ^ symbol bitterness appears in the first position of the square brackets, then matches any character not in the list.


A string that consists of consecutive characters that do not contain special characters is called a word:

\<: The first word appears on the left side of the word, \b

\<char

\>: Ending, appearing on the right side of the word, \b

Char\>


To extend the regular expression:

Character Matching:

.

[]

[^]

Number of matches:

*: Any time

?: 0 or 1 times

+: at least 1 times

{m}: exact match m times

{n,m}: \{n\} refers to reproducing n times; \{n,m\} refers to reproducing N to M times

{m,}: at least m times

{0,n}: up to n times

Anchoring:

^

$

\<, \b

\>, \b

^$, ^[[:space:]]*$

Group:

()

Cited by: \1, \2, \3




Or:

A|b:a or B

Con (c|c) at

Concat or concat?

Conc or Cat



Grep-e ' PATTERN ' FILE ...

Egrep ' PATTERN ' FILE ...



notation in common Linux/unix tools

Pcre notation

Vi/vim

Grep

Awk

Sed

*

*

*

*

*

+

\+

\+

+

\+

?

\=

\?

?

\?

{M,n}

\{m,n}

\{m,n\}

{M,n}

\{m,n\}

\b *

\< \>

\< \>

\< \>

\y \< \>

(..... | ...)

\ (... \|...\)

\ (... \|...\)

(..... | ...)

(..... | ...)

(...)

\(...\)

\(...\)

(...)

(...)

\1 \2

\1 \2

\1 \2

Not supported

\1 \2

Note: Pcre commonly used in \b to denote "the beginning or end of a word", but Linux/unix tools usually use \< to match the "starting position of a word" and \> to match "end position of word", and \y in SED can match both positions simultaneously.


Instance:

1, displays the lines in the file that begin with uppercase or lowercase C





2, displays the lines in the file that begin with R






3, display the end of the file line at (2)







4, there are 2-3 o strings in the search file between R and T





5, search repeats one or more of the strings as



6,ifconfig command can display the current host IP address related information, etc., if using grep and other text processing commands to remove the IP address of the machine, the request does not pack 127.0.0.1;




This article is from the "Share with June" blog, please be sure to keep this source http://kevin92.blog.51cto.com/6928681/1600656

Linux command grep, egrep, regular expression Daquan

Related Article

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.