Linux Learning---Regular expressions and grep

Source: Internet
Author: User

Regular Expressions (Regular expression (RE))

is through the arrangement of some special characters to "search/replace/delete" one or more columns of text string, in short, the regular expression is used in the processing of a string "expression" above. Regular expressions are not a tool program, but a standard basis for string processing, and if you want to use regular expressions to handle strings, you will need a tool program that supports regular expressions, such as VI, awk, grep, sed ...

Grep

Basic rules:

grep ' Search String ' filename

Options and Parameters:

-A: Search data in binary archives in text file mode

-C: Calculates the number of ' search string ' found

-I: Ignore case

-N: Output line number

-V: Reverse selection, which displays rows without a ' search string ' content

Example

1. Remove the line that appears root in the last

 Last grep ' Root '

2. As opposed to 1, as long as there is no root line

 Last grep ' Root '

3. Remove the/etc/man.config containing the Manpath and display the line number

grep ' MANPATH ' -n/etc/man. config

Plus the--color=auto option, the keywords you find are displayed in a special color.

Advanced

Rules:

grep ' Search String ' filename

Options and Parameters:

-A: The following can be added to the number, in the meaning of After, in addition to listing the row, the subsequent n rows are also listed

-B: The following can be added as a number, for the meaning of befer, in addition to listing the row, the previous n rows are also listed

Example

1.

grep ' ETH '

The DMESG can list the core-generated messages and access the network card-related information via grep (ETH).

2. With the title, the first two lines of the keyword line and the last three lines are also displayed:

grep ' ETH '

3. Use the brackets [] to search for set characters

If you want to search for test or taste these two words, you can find that they have a common ' t?st ' existence, this time,
I can search for this:

grep ' t[ae]st ' filename

4. We can use,

grep ' oo ' filename

To search for a line that contains ' oo ', so maybe or search for a bunch of words good, food ... Line, what if we don't want ' oo ' in front of ' G '? You can use the inverse selection of the set character [^]:

grep ' [^g]oo ' filename

If you don't want ' oo ' to have lowercase letters, we can write this:

grep -n [^a-z]oo'  filename

That is, for consecutive females or numbers, you can write this [a-z], [A-z], [0-9].

< not finished >

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.