Grep usage and regular Expressions (i)

Source: Internet
Author: User

Grep usage and regular Expressions (i)

Learning Goals

Usage of grep

Why study the regular expression?

Learn more meta-characters

Flexible application of metacharacters

1. Use of grep

ls | grep "Find the Content" grep "Find the Class tolerance" | a file;

Grep–-version View the version number of grep;

Cat Hello.txt | grep "A" finds out a;

Grep-v "A" test.txt except A, others are found out;

Grep-c "A" test.txt see how many rows a has;

Grep-v-C "a" test.txt see how many rows except A;

grep--color "A" test.txt see all the contents of a, and a is shown in red!

Grep-n "A" test.txt view all the contents of a, and display line numbers!

Grep-n–a 2–-color "A" test.txt displays the rows containing a content in color, and displays the next two lines of the row that includes the a content;

Grep-n–b 2–-color "A" test.txt displays the rows containing a content in color, and displays the upper two lines of the line that includes the a content;

Grep-n–c 2–-color "A" test.txt displays the rows containing a content in color, and displays the upper and lower lines of the rows that include a content;

2. Why learn regular expression (regular expressions)

(1) Fuzzy, fast matching data string

(2) laying the groundwork for learning grep, awk, sed and other text filtering commands

(3) The need to write advanced bash scripts

3. What meta characters are there?

[] \ ^ $ . | ? * + ( )

4. Basic meta-character set and its meaning

Example:

(1) ^ Match beginning of Line

ls | grep ' ^[0-3] ' query out all files beginning with 0~3;

ls | grep ' ^[^0-3] ' queries out all files that are not 0~3;

(2) $ match line end

Cat Test.txt | grep "^[0-3]e$" matches the content beginning with 0-3, e ending;

(3) * A single character followed by *, matching 0 or more of this character;

(4)? Match 0 or 1 times;

Cat Test.txt | grep "^[0-9]?*"

(5). Match only any single character

Cat Test.txt | grep "5.E"

(6) Pattern \{n\}

The difference between the two: the first match: does not necessarily match the beginning of the two numbers appear;

Second match: Two digits at the beginning of the match appear;

(7) Pattern \{n,\} appears at least n times

(8) The number of pattern \{n,m\} appears between N~m;

Grep usage and regular Expressions (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.