A detailed description of grep usage in Linux

Source: Internet
Author: User

Find a specific string and color display
[Email protected] test]# grep-n ' the ' Regular_express.txt--color=auto
8:i can ' t finish the test.
12:the symbol ' * ' is represented as start.
15:you is the best was mean you are the No. 1.
16:the World <Happy> was the same with "glad".
18:google is the best tools for search keyword.

Reverse select to find a specific string and color display

[Email protected] test]# grep-vn ' the ' Regular_express.txt--color=auto
1: "Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
......

Ignore case to find specific characters

[email protected] test]# grep-in ' the ' regular_express.txt

Use [] to find the collection character:

[Email protected] test]# grep-n ' t[ae]st ' regular_express.txt
8:i can ' t finish the test.
9:oh! The soup taste good.

Look for a ' oo ' string

[Email protected] test]# grep-n ' oo ' regular_express.txt
1: "Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:football game isn't use feet only.
9:oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!

Look for the ' oo ' string, but do not have g in front, that is, eliminate goo

[[email protected] test]# grep-n ' [^g]oo ' Regular_express.txt
2:apple is my favorite food.
3:football game isn't use feet only.
18:google is the best tools for search keyword.
19:goooooogle yes!

Find content with non-lowercase letters and OO

[[email protected] test]# grep-n ' [^a-z]oo ' Regular_express.txt
3:football game isn't use feet only.


Get a row with a number

[Email protected] test]# grep-n ' [0-9] ' regular_express.txt
5:however, this dress was about $3183 dollars.
15:you is the best was mean you are the No. 1.

[[email protected] test]# grep-n ' [^[:lower:]]oo ' Regular_express.txt
3:football game isn't use feet only.

Query for characters that begin with the

[Email protected] test]# grep-n ' ^the ' regular_express.txt
12:the symbol ' * ' is represented as start.

The query starts with a lowercase letter content

[Email protected] test]# grep-n ' ^[a-z] ' regular_express.txt
2:apple is my favorite food.
4:this dress doesn ' t fit me.
10:motorcycle is cheap than car.
12:the symbol ' * ' is represented as start.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! Go! Let ' s go.

Query the first character is not uppercase

[Email protected] test]# grep-n ' ^[[:lower:] ' regular_express.txt
2:apple is my favorite food.
4:this dress doesn ' t fit me.
10:motorcycle is cheap than car.
12:the symbol ' * ' is represented as start.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! Go! Let ' s go.

Query for characters that are not beginning with English

[Email protected] test]# grep-n ' ^[^a-za-z] ' regular_express.txt
1: "Open Source" is a good mechanism to develop programs.
21:# I am Vbird

The query ends with the contents of the line of the decimal point.

[Email protected] test]# grep-n ' \.$ ' regular_express.txt
1: "Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:football game isn't use feet only.
4:this dress doesn ' t fit me.
10:motorcycle is cheap than car.
11:this window is clear.
12:the symbol ' * ' is represented as start.
15:you is the best was mean you are the No. 1.
16:the World <Happy> was the same with "glad".
17:i like dog.
18:google is the best tools for search keyword.
20:go! Go! Let ' s go.

Find "Blank line"

[Email protected] test]# grep-n ' ^$ ' regular_express.txt
22:

Wildcard characters. and * use,. (decimal point) The representative must have an arbitrary character, * representing the meaning of repeating the previous one to an infinite number of times

[Email protected] test]# grep-n ' G.. d ' Regular_express.txt
1: "Open Source" is a good mechanism to develop programs.
9:oh! The soup taste good.
16:the World <Happy> was the same with "glad".

Query for any number of rows that appear

[ [email protected] test]# grep-n ' [0-9][0-9]* ' Regular_express.txt
5:however, this dress was about $3183 dollars.
15:you is the best was mean you are the No. 1.

The query appears with a string of two O

[[email protected] test]# grep-n ' o\{2\} ' regular_express.txt
1: "Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:football game isn't use feet only.
9:oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!

The query appears with a string of 2-5 O, followed by a G string

[Email protected] test]# grep-n ' o\{2,5\}g ' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!

The query appears with a string of more than 2 O, followed by a G string

[Email protected] test]# grep-n ' o\{2,\}g ' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!




A detailed description of grep usage in Linux

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.