Usage of grep in linux

Source: Internet
Author: User

Usage of grep in linux

Search for a specific string and display the color
[Root @ fwq 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 are the best is mean you are the no. 1.
16: The world <Happy> is the same with "gglad ".
18: google is the best tools for search keyword.

Select to search for a specific string in reverse direction and display the color

[Root @ fwq test] # grep-vn 'the 'regular_express.txt -- color = auto
1: "Open Source" is a good mechanic to develop programs.
2: apple is my favorite food.
......

Ignore case sensitive characters

[Root @ fwq test] # grep-in the 'regular_express.txt

Use [] to find the collection characters:

[Root @ fwq test] # grep-n't [AE] st'regular_express.txt
8: I can't finish the test.
9: Oh! The soup taste good.

Search for strings with 'oo'

[Root @ fwq test] # grep-n 'oo 'regular_express.txt
1: "Open Source" is a good mechanic to develop programs.
2: apple is my favorite food.
3: Football game is not use feet only.
9: Oh! The soup taste good.
18: google is the best tools for search keyword.
19: goooooogle yes!

Find a string with a 'oo ', but do not have a g before, that is, remove goo

[Root @ fwq test] # grep-n' [^ g] oo 'regular_express.txt
2: apple is my favorite food.
3: Football game is not use feet only.
18: google is the best tools for search keyword.
19: goooooogle yes!

Search for non-lowercase letters and oo content

[Root @ fwq test] # grep-n' [^ a-z] oo 'regular_express.txt
3: Football game is not use feet only.

Obtain a row with numbers

[Root @ fwq test] # grep-n '[0-9] 'regular_express.txt
5: However, this dress is about $3183 dollars.
15: You are the best is mean you are the no. 1.

[Root @ fwq test] # grep-n '[^ [: lower:] oo 'regular_express.txt
3: Football game is not use feet only.

Query the characters starting with

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

Queries content starting with lowercase letters

[Root @ fwq 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.

The first character of the query is not in uppercase.

[Root @ fwq 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 is not a character that starts with English

[Root @ fwq test] # grep-n' ^ [^ a-zA-Z] 'regular_express.txt
1: "Open Source" is a good mechanic to develop programs.
21: # I am VBird

The content of the row ending with the decimal point.

[Root @ fwq test] # grep-n' \. $ 'regular_express.txt
1: "Open Source" is a good mechanic to develop programs.
2: apple is my favorite food.
3: Football game is not 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 are the best is mean you are the no. 1.
16: The world <Happy> is the same with "gglad ".
17: I like dog.
18: google is the best tools for search keyword.
20: go! Go! Let's go.

Search for blank lines"

[Root @ fwq test] # grep-n' ^ $ 'regular_express.txt
22:

Wildcard. And * usage,. (decimal point) indicates that there must be an arbitrary character, * Indicates repeating the previous one to an infinite number of times.

[Root @ fwq test] # grep-n 'G .. d' regular_express.txt
1: "Open Source" is a good mechanic to develop programs.
9: Oh! The soup taste good.
16: The world <Happy> is the same with "gglad ".

Query the rows and columns with any numbers

[Root @ fwq test] # grep-n '[0-9] [0-9] * 'regular_express.txt
5: However, this dress is about $3183 dollars.
15: You are the best is mean you are the no. 1.

Query strings with two o Records

[Root @ fwq test] # grep-n' o \ {2 \} 'regular_express.txt
1: "Open Source" is a good mechanic to develop programs.
2: apple is my favorite food.
3: Football game is not use feet only.
9: Oh! The soup taste good.
18: google is the best tools for search keyword.
19: goooooogle yes!

Query the strings with 2-5 o Records, followed by a string of g.

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

Query strings with more than 2 o Records followed by a string with a g ID

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

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.