Regular Expressions and grep

Source: Internet
Author: User
Tags egrep

Part three regular expressions and grep

This chapter mainly through some application examples, to explain the regular expression.

1 Regular Expressions

A regular expression is an expression of a string. It can represent one or more columns of strings with meaningful special symbols.
grep is a regular expression tool commonly used under Linux systems, and you can use grep to retrieve strings for input streams such as text.

2 Regular Expression special symbols

Refer to the table below

3 grep Expressions

Basic Format
grep [OPTIONS] PATTERN [FILE ...]
Format description
Pattern: Match pattern. Can be a string, or it can be a regular expression.
[FILE ...] : Is the file (set) of grep search
[Options]: is the option for grep. Common options have the following options.
-C: Outputs only the count of matching rows.
-I: Case insensitive (only for single-character).
-H: The file name is not displayed when querying multiple files.
-L: Only file names that contain matching characters are output 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.
- R: When file contains a folder name, all subdirectories of that folder are traversed, and subdirectories are not traversed by default.

4 Application Examples

The following is an example of Input.txt, which describes grep. The text content of Input.txt is as follows:

"Open Source"is a good mechanism to develop programs.apple are my favorite food. Football game isn't use feet only.this dress doesn‘T fit me. However, this dress was about $3183 dollars.^Mgnu isFree Air notFree beer.^Mher hair is very beauty.^MI can‘T finish the test.^moh! The Soup taste good.^Mmotorcycle is cheap than car. This window isClear. The symbol ' *  is represented as start. Oh! my god! the GD software is a library for Drafting Programs.^myou was the best was mean you were the No. 1 "glad ". I like Dog.google are the best tools for search Keyword.goooooogle yes! go! go! Let ' s go       

(01), find the line containing "the", and display the line number.

"the" Input.txt 

Description:-n means the line number is displayed

(02), case-insensitive, find rows that include "the", and display line numbers.

grep-"the" Input.txt  

Description:-n means the line number is displayed;-I means case-insensitive, that is, ignore case.

(03), to find rows that do not include "the", count the number of rows.

"the" Input.txt 

Description:-C Represents a statistic (count), and-V indicates an item that does not match.

(04), find the file containing "the" in "Current directory" and "All subdirectories", and display the line number of "the" in it.

"the". 

Description:-r means recursive lookup;-n indicates the line number is displayed.

(05), find the item that matches "T?st", where? is any character.

"t.st" Input.txt 

Description:. means match any character

(06) To find rows that contain numbers

"[0-9]""[[:d igit:]]     

Description: [0-9] Represents a number between 0-9 and [[:d Igit:]] Also represents a number between 0-9

(07), find the line that begins with the

"^the" Input.txt 

Description: "^the" means starting with the

(08), find the line that ends with a lowercase letter.

"[a-z]$" Input.txt 

Description: [A-z] represents a lowercase letter, $ represents a terminator, and [a-z]$ represents an item that ends in a lowercase letter.

(09) To find a blank line.

"^$" Input.txt 

Description: ^ denotes the beginning, such as ^t, which begins with the letter T; $ represents the end, as e$ means ending with E. ^$ represents a blank line.

(10) To find words that begin with the letter G

"\<g" Input.txt 

Description:\< represents the beginning of a word, and \<g represents a word starting with G.

(11), find the word "go" for the string. Note: Strings such as Goo,good cannot be included

"\<go\>" Input.txt 

Description:\< Indicates the beginning of a word,\> means the end of the word. \<go\> denotes the letter G, ending with the letter O.

(12), find a line that includes 2-5 letters O.

"o\{2,5\}" Input.txt 

Description: Pattern\{n,m\} represents n to M pattern. O\{2,5\} represents 2-5 letters O.

(13), find rows that include more than 2 letters O (including 2).

"ooo*""oo\+""o\{2,\}" Input.txt       

Description
Ooo*: The front two oo means match 2 letter O, and the following o* indicates matching 0 to multiple letters O.
oo\+: The first letter o means matching a single letter o; the last "o\+" works together, where the \+ is an escaped +, representing 1 to more, while o\+ represents 1 to more letters O.
Pattern\{n,\} indicates more than n pattern. O\{2,\} means more than 2 letters O.

4 Egrep4.1 egrep Description

Egrep is an extended grep, meaning it functions more than grep. "Egrep" is equivalent to "GREP-E".
Egrep with grep, supports parentheses "()" And operator "|" (expressed or).

4.2 Egrep Application Example

Still use the above input.txt as a description of the input text

(01), find the line containing the or this

"the|this" Input.txt 

Description:-n Indicates the line number of the output match, and "the|this" means the item that includes the.

(02), find the line containing the or this

"(the|this)" Input.txt 

Description:-N Represents the line number of the output match, and "the|this" means the item that includes the or this, and-V represents the opposite of the match. That is,-V "the|this" means that neither the item nor the this is included.

Regular Expressions and grep

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.