The grep and regular expression of Linux learning

Source: Internet
Author: User

1. grep: Search for text based on the pattern and print the line of text that conforms to the pattern

commands to filter text using patterns defined by basic regular expressions

grep Option mode (regular expression) parameter

Pattern: A matching condition formed by the combination of meta-characters of a text-word wildcards regular expression

-e: extending regular expressions

-I: Case insensitive

-W: Matches entire word

-x: Match entire row

-V: Show rows that do not match

--colour/--color: Highlighting matching strings

-A: Print the match itself and several subsequent lines

-B: Print the match itself and the previous lines

-C: Print a few lines that match the back and forth of the machine

-O: Show only the matching string

-C: show how many matches

-N: Show line numbers at the beginning


2, Regular expression: divided into basic regular expressions and extended regular expressions

Basic REGEXP (REgular expression): A logical formula for the manipulation of strings, which is to make a "rule string", a "ruleset", with a predefined set of specific characters, and a combination of those specific characters. Used to express a filtering logic for a string.

metacharacters: Refers to characters that have a special meaning in a regular expression, and the meta-character and Shell wildcard characters are basically the same, but some characters have different meanings

The regular expression defaults to a greedy pattern match (as long as possible to match)

' search string ' is a regular expression, in order to avoid the effect of Shell's metacharacters on regular expression, use ' single quotation marks, avoid using ' ' double quotation marks, sometimes omit quotation marks

. Match any single character (used in shell wildcard?) to indicate)

[] matches any single character within the specified range

[^] matches any single character outside the specified range (same as the shell wildcard character)

Character Set:

[:d igit:] Number

[: Lower:] lowercase letters

[: Upper:] Uppercase

[:p unct:] Punctuation

[: space:] Space

[: Alpha:] uppercase and lowercase letters

[: alnum:] numbers and uppercase and lowercase letters

number of matches:

* Match its preceding character any time, can be 0 times (shell wildcard symbol matches any character of any length)

. * Match any character any time

\? Match its preceding character 1 or 0 times

\{m,n\} matches the characters preceding it at least m times, up to N times

Location Anchoring:

^ Anchor Line, any content after the second character must appear at the beginning of the line

$ anchor Line end, any content in front of the second character must appear at the end of the row

^$ Blank line

\< or \b anchor Word, any character following it must appear as the head of the word

\> or \b anchors the ending, any character preceding it must appear as the tail of the word

Group: Back-to-reference functionality,

\(\)

\1 refers to the first parenthesis and all the contents of the corresponding closing parenthesis

\2

\3

Extended Regular expression (EXtended REGEXP): Egrep (matches extended regular expression)

Use the GREP-E or EGREP command (Egrep is no longer used in some distributions, use GREP-E)

Meta-character meanings are the same in extended regular expressions as basic regular expressions

Matches more than one meta-character +, representing the characters before it matches at least once

Group (); more explicit than basic regular expressions

The introduction or concept of a | representation

For example, a|b represents A or B, (a| B) C on behalf of AC or BC

Fgerp:fast Gerp (regular expression not supported)

Command options are the same as grep

Unlike grep and Egrep, the FGERP command uses a fast compression algorithm because he searches for a string instead of a pattern that matches an expression. When working with large files, Fgerp is faster than grep, of course fgerp is naturally weaker than grep.

This article is from the "Pole Men" blog, please be sure to keep this source http://wtime.blog.51cto.com/8829658/1530763

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.