Linux grep and regular expressions

Source: Internet
Author: User
Tags control characters egrep

grep command

format: grep [-ACINV] [--color=auto] ' Find string ' filename

-A converts binary files to text file

-C calculates the number of ' find string ' found

-I ignores case differences, so case is considered the same

-N Output line number

-V reverse selection, which shows the line without the ' Find string ' content

--color=auto key section plus color display

The basic regular

^: Beginning of the line

$: End of line

Find the beginning of the line with 0:

Find the end of the line with 3:

. Represents a character that must have an arbitrary character

\ escape character, remove the special meaning of special characters

* Repeat 0 to an infinite number of previous characters (be sure to remember is 0 to infinity, if you want grep with ' 3 ' line, with ' * * ' is invalid, should be ' 33* ', in fact grep ' 3 ' = grep ' 33* ')

[List], [ABC] or A or B or c,[0-9] is any of the 0-9 range, similarly [a-z] or [a-z]

[^list] takes a character that is not within that range, and the following example, grep a line that is a whole line of non-lowercase alphabetic characters. * For 0 to an infinite number of meanings, if there is an extended regular expression, with + can express 1 to 1 or more.

\{n,m\} \{n,\} \{\,m}

Extending regular Expressions

Be sure to note that extended regular expressions are written as Egrep

+ Previous * Corresponds to one or more of the previous characters

Egrep ' ^[^a-z]+$ ' = grep ' ^[^a-z][^a-z]*$ '

? 0 or one of the previous characters

| Or

() a set of characters

() + Multiple repeating groups

Identifies the character set, for example, [:d Igit:] instead of [0-9] instance: Egrep-n ' ^ (12|2[[:d igit:]][[:d igit:]]| ( 29|30|31|32) [[:d igit:]][[:d Igit:]] $ ' file*

(Find n,n=12 or 200<=n<=299 or 2900<=n<=3299)

[: Alnum:] Letters and numbers
[: Alpha:] \a Letter
[: Lower:] \l Small Letter
[: Upper:] \u Capital Letter
[: Blank:] white space characters (spaces and tabs)
[: Space:] \s all whitespace (than [: blank:] contains a wide range)
[: Cntrl:] non-printable control characters (backspace, delete, alarm ...) )
[:d igit:] \d decimal digits
[: xdigit:] \x hex digit
[: Graph:] printable non-whitespace characters
[:p rint:] \p printable character
[:p UNCT:] Punctuation

Finally, grep can fetch characters from multiple files, grep-n ' regular expression ' file1 file2

Linux grep and regular expressions

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.