grep and regular expressions

Source: Internet
Author: User
Tags printable characters

Regular Expression Sample table
Example of character meaning
* string of any length. A * means: empty string, AAAA, a ...
? A string of length 0 or 1. A? Represents: An empty string and a.
+ length is one or more strings. A + says: A, AA, aaaaaa ...
. Any character. A. Indicates: A is followed by any character.
{} represents the number of repetitions of the previous rule,
{1,1,s} contains a set of matching curly braces with two numbers and one character representing the characters found within a specified number of times. A{3} means: three A,
a{1,3} means: One to three A,
A{3,} means: greater than or equal to three A,
{3,7,a} represents the match character a within the range of 3 to 7 repetitions.
[] collection, which represents any one of the characters in square brackets. [AB] means: A or B is OK,
[A-Z] indicates: characters from A to Z.
() group, which represents a set of characters. (AB) {2} means: Abab.
A/b is also met. A/b means that string A is followed by string B to satisfy the requirement.
A|b, representing a or conforming to B, can be a|b indicated that either the string A or the string B meet the requirements.
^ If placed at the beginning means that the rule must be at the beginning of the string, other positions represent the character itself.
When placed at the beginning of [] means that the collection is reversed, other locations represent the character itself. ^a says: A must be at the beginning of the string,
[^a] means: Except for a character other than a.
$ if put last indicates that the rule must be placed at the end, other locations represent the character itself. a$ says: A must be at the end of the string.
\:s Regular Expressions use \:s to represent spaces. A\:SB matches a B.
\:A Regular Expressions use \:a to represent characters and numbers. A\:a Match AB, A6 and so on.
\:C Regular Expressions use \:C to represent only characters. A\:C matching AC, etc., does not match A1, etc.
\:p Regular expressions use \:p to represent printable characters.
\:D The regular expression uses \:d to represent numbers only. A\:C matching A1, etc., does not match AC, etc.
The \:x00 regular expression uses \:x00 to represent ASCII characters.
The \:R regular expression uses \:r to indicate a carriage return.
\:n Regular expressions use \:d to represent line breaks.

A regular expression
Basic meta character set and its meaning
^ only matches the beginning of the line
$ only matches end of line
* A single character followed by *, matching 0 or more characters
[] only matches [] inside characters. Can be a single character, or it can be a sequence of characters. can be used-
denotes [] The range of characters within a sequence, as [1-5] instead of [1 2 3 4 5]
\ is used only to mask the special meaning of a meta-character. Because sometimes some meta characters in the Shell have
Special meaning. \ can make it lose its meaning
. Match only any single character
The pattern\ is used only to match the number of preceding pattern occurrences. n is the number of times
Pattern\m only the same as above, but the least number of times is n
Pattern\ only has the same meaning, but the pattern occurs between N and M

Usage of two grep
1 double quotation mark reference
When you enter a string parameter in the grep command, it is best to enclose it in double quotation marks.
2 grep options
The common grep options are:
-C outputs only the count of matching rows.
-I is case-insensitive (only for single-character).
-H does not display a file name when querying multiple files.
-L Only output file names that contain matching characters when querying multiple files.
-N Displays matching lines and line numbers.
-S does not display error messages that do not exist or have no matching text.
-V Displays all lines that do not contain matching text.

- o Print each match, but only the match, not the entire line.


3 Exact Match
A more efficient way to extract exact matches using grep is to add \> after extracting the string. Assuming that 48 is now accurately extracted, it is "48\>"
Tri-grep and regular expressions
It is best to use single quotes when using regular expressions, which prevents the proprietary patterns used in grep from being confused with the special ways of some shell commands.
1 Mode Range
Assuming that you want to extract a city location with code 484 and 483, the previous chapter mentions that you can use [] to specify a string range, starting with 48,
End with 3 or 4, and then extract 484 or 483. grep ' 48[34] ' DATA.F
2 does not match the beginning of the line
If you want to extract the record so that it is not 48 at the beginning of the line, you can use the ^ mark in square brackets to indicate that the query starts at the beginning.
grep ' ^[^48] ' DATA.F
3 Match any character
If you extract all the code that starts with L and ends with D, you can use the following method because the code length is known to be 5 characters:
grep ' L ... D ' DATA.F
4th-Year Enquiry
A common query pattern is a date query. All records beginning with 5 starting at 1 9 9 6 or 1 9, 9 8, are queried first. Use Mode 5: 199[6,8]. This means that the first character is 5, followed by two points, followed by 199, and the remaining two digits are 6 or 8.
grep ' 5..199[6,8] ' DATA.F
Another way to query all records that contain 1998 is to use the expression [0-9]\[8], meaning that any number repeats 3 times, followed by the number 8, although this method is not as precise as the previous method, but it also has some effect.
5 using grep to match "and" or "or" mode
The grep command plus-e parameter, which allows extended pattern matching to be used. For example, to extract a city code of 2 1 9 or 2 1 6, the method is as follows:
Grep-e ' 219|216 ' DATA.F
6 Blank Line
Use a combination of ^ and $ to query for blank lines. Use the-n parameter to display the actual number of rows:
grep ' ^$ ' DATA.F
7 grep and the use of class names
Regular expressions equivalent to class-equivalent regular expression classes
[[: Upper:]]               [A-z] [[: Alnum:]] [0-9a-za-z]
[[: Lower:]]               [A-z] [[: Space:]] space or t a B key
[[:d Igit:]] [0-9] [[: Alpha:]] [a-za-z]

grep and regular expressions

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.