grep & Regular Expressions

Source: Internet
Author: User

A regular expression is just a description of a string, only combined with a tool that supports regular expressions for string processing. Vim, grep, awk, and sed all support regular expressions, and it is because they support the regular that they are powerful;

grep command

Function: Finds a string in each line of the input file.

Basic usage: grep [-ACINV] [--color=auto] [-A n] [-B N] ' search string ' file name

Parameter description:

    • -A: Binary documents are processed in text mode
    • -C: Show number of matches
    • -I: Ignore case differences
    • -N: Show line numbers at the beginning
    • -a:after, displays data that matches n rows after the string
    • -b:before, displays data that matches the first n rows of a string
    • -V: show no matching rows
    • -a:after meaning, display the match part after n rows
    • -b:before means that the matching part is displayed before the N line
    • --color: Highlight matching keywords in a specific color

The –color option is a great option for you to clearly understand which characters are matched. It is best to include in your own. BASHRC or. bash_profile file:

Alias Grep=grep--color=auto

after each grep search, the matching effect is automatically highlighted.

The search string ' is a regular expression, note that in order to avoid the effect of the shell's metacharacters on the regular expression, enclose it in single quotation marks (""), and never enclose it in double quotation marks ("").

Regular expressions are divided into basic regular expressions and extended regular expressions. Here is a brief summary of each.

Basic Regular Expressions

Regular Expression learning is mainly about regular expression meta-data learning. There is nothing advanced in the regular expression itself, this article simply summarizes the meta-data of the basic regular expression:

Meta data

Meaning and examples

^word Searches for lines that begin with Word.

For example: Search for script comment lines that begin with #

Grep–n ' ^# ' regular.txt

word$ Search for lines ending in Word

For example, search for '. ' End of Line

Grep–n '. $ ' regular.txt

. matches any one character.

Example: Grep–n ' E.E ' regular.txt

Matches between E and E have any one character, can match eee,eae,eve, but does not match the EE.

\ The escape character.

For example: Search ', ' is a special character that has a special meaning in the regular expression. Must be escaped first.

Grep–n ' \ ' Regular.txt

* The preceding character repeats 0 to several times.

such as matching gle,gogle,google,gooogle, etc.

Grep–n ' Go*gle ' regular.txt

[List] Matches one of a series of characters.

For example: Match GL,GF.

Grep–n ' g[lf] ' regular.txt

[N1-N2] Matches one character in a range of characters.

Example: Match numeric characters

Grep–n ' [0-9] ' regular.txt

[^list] Match characters outside the character set

Example: Grep–n ' [^o] ' regular.txt

Match non-O characters

\{n1,n2\} The preceding character repeats n1,n2 times

For example: Match Google,gooogle.

Grep–n ' Go\{2,3\}gle ' regular.txt

\<word The word is the beginning.

Example: Match a word that starts with G

Grep–n ' \<g ' regular.txt

Word\> Match end of Word

For example: match words ending with tion

Grep–n ' tion\> ' regular.txt

Extending regular Expressions

grep generally supports basic regular expressions, which can be extended with parameter-e support, and grep provides an extension command called Egrep to support extended regular expressions, which is equivalent to GREP-E. In general, though, the basic regular expression is sufficient. In special cases, complex extended expressions can simplify the matching of strings.

The extended regular expression is based on the basic regular expression, and adds some meta-data.

meta data

Meaning and example

+ Repeat the preceding character 1 to multiple times.

For example: Match God,good,goood and so on string.

Grep–ne go+d ' regular.txt

? matches characters preceding 0 or 1 times

For example, matching gd,god

Grep–ne ' go?d ' regular.txt

| For example: Grep–ne ' God|good ' regular.txt

matches god or good.

() matches the entire bracketed string, which turns out to match a single character

For example: Search good or glad

Grep–ne ' g (oo|la) ' regular.txt

() such as matching gle,gogle,google,gooogle, and so on

Grep–ne ' go*gle ' regular.txt

The regular expressions under Linux are profound, the above support summarizes the most commonly used parts, if proficient in the above part of the regular expression can basically meet the daily use.

In addition, many Linux commands support regular expressions, such as Find,sed,awk and so on. Please use regular expressions when using the manual reference to these commands.

Reference: http://blog.csdn.net/deyili/article/details/5548603

Http://www.cnblogs.com/xuxm2007/archive/2011/06/15/2081671.html

grep & 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.