Easy-to-use regular expression Grep,sed,awk (i)

Source: Internet
Author: User

Some nonsense:

For regular expressions, always like to understand not understand, see some code, script regularly,awk,sed. The heart is always a bit empty. I can't remember the main. Usually not how to use, also did not summarize.

Now have time, decided to summarize, by the way to overcome the see shell,js These with regular on the weakness of the guilty.

I'm going to write three essays.

Text :

Generally speaking, there are several kinds of regular. But basically almost the same, what I know is"Basic Regular","Extended Regular"andThe PerlRegular",This article is more inclined to say record"Basic Regular"and the"Extended Regular", they areGrep,egrep,sed,awkplay an important role in the

Before you start, review Some of the regular usage parameters of grep :

-N, which displays the line number. -V, Reverse Select-I, ignore uppercase and lowercase

In order to come down to transcription, the reference to "Brother Bird'sLinuxPrivate Cuisine.Basic Learning Chapter ". Download the text we want to use first:

Http://linux.vbird.org/linux_basic/0330regularex/regular_express.txt

The contents are as follows:


Learn the basic rules Form First



1. Direct Matching

Example 1: find the text with Apple and is separately


This should be the simplest use of regular.



2. square brackets []

[] is primarily a collection-related match. The use of the method we still use examples to illustrate

Example 2 matching text containing test,tast


Can see. [] is to select one from the set [AE] to match



3. square brackets [] and Middle horizontal lines - combine

We can write [0123456789] to match a text that contains a number, but it's too cumbersome, which requires a horizontal line for numbers that can be written in [0-9], the Same, It can also be applied to letters, uppercase letters [A-z], lowercase letters [A-z], and can be combined. such as uppercase and lowercase letters [a-za-z].

Example 3, Find the text containing the number



4. square brackets [],-, ^, three combinations

appear in [] ^ to reverse, for example

Example 4. Remove with oo . But the text that does not contain g before oo


The last "19:goooooogle yes!" Why does it match up? Although the front is goo, obviously not satisfied. But. Go (oo) oogle, is satisfied, so match up on it. This may be one of the difficult points in the regular, and the regular you write may be a bug, but you haven't found it yet.

Example 5. Matches the text with OO. But oo does not contain lowercase letters before

See, this is [],-, ^ Common Use, note : ^ in [] Necay means to take the inverse.


5.^ and $

There's another ^ here, but not the same as above. Here ^ represents the beginning of the line , the corresponding $ represents the end of the row.

Example 6 remove The text that begins with the


Example 7 Remove text ending with a number or letter


Example 8 Removing a blank line


Empty lines are matched with ' ^$ '


6. Point number . with asterisks *

Point number . indicates that there is only one random character

An asterisk indicates that the previous 0 or more characters are repeated


Example 9 match shape like g?? D 's string ( two characters between GD)


As seen in the results. Point number . is to represent a random character.


The example matches a minimum of two consecutive o characters.


Notice here. "*" is not the same as the wildcard character * that we know .

Example of a text that matches the end of G beginning g


It is not possible to use ' g*g ' . Because * and wildcards are not the same, the correct is ' g.*g '

So , remember, the regular form of the * and the wildcard * is not the same!


7. Escape \

Let's say that we want to match the text to represent some special characters ("bird Brother's Linux private dish" in the shell has a special meaning, I think it is wrong, or misleading, Is it just in the shell? For example, what does he mean by the sample point, which is the folder in the shell? In fact, the real reason is. is the regular expression character? ), what should I do? Escape.

such as the match with the dot number . the text at the end. We know . a match is represented in a regular table and matches only a random character, so you can use the ' \.$ '


Let's write the main statement today.


References:

"Brother Bird's Linux private dish"

"Linux Programming"

http://www.ibm.com/developerworks/cn/education/aix/au-unixtips3/

Http://www.cnblogs.com/chengmo/archive/2010/10/10/1847287.html

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Easy-to-use regular expression Grep,sed,awk (i)

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.