The shell regular expression and grepfindawksed detailed explanation

Source: Internet
Author: User
Tags expression lowercase range regular expression

Shell regular Expressions and grep awk find sed sort uniq cut

One, grep

1, basic parameters

GREP-[ACINV] ' search content string ' filename

-A text file search

-C calculates the number of matched rows found

-I ignores case

-N to output the line number in passing

-V reverse selection, that is, to find a line without a search string

[Root@test3 ~]# cat Travel.doc Travel is a good way to refresh and broaden our horizon.
During your travel, can turn off your cellphone and keep to the Internet. You can forget your work, your study or your family, and just the enjoy time.
From the moment when you start your journey, the trifles should is locked at your house.
During the travel, you can kill your time slowly to have a good rest, and your can contacts with different people.
They May is a window for your to know a different world.
Meet various people, experience things, which make your world bigger.
You are just clean up your entire mind and you can get the true meaning of travel.
Find the line and print line number [root@test3 ~]# grep-n "the" Travel.doc 5:they May is a window for your to know a different world.
Search does not have the line [root@test3 ~]# grep-vn "The" Travel.doc 1:travel is a good way to refresh and broaden our horizon.
2:during your travel, can turn off your cellphone and keep to the Internet. 3:you can forget your work, your study or your family, and just enjoy the leisure time.
From the moment when you start your journey, the trifles should is locked at your house.
4:during the travel, you can kill your time slowly to have a good rest, and your can contacts with different people.
6:meet various people, experience things, which make your world bigger.
7:you just clean up your entire mind and your can get the true meaning of travel. Calculates the number of rows that contain travel words, case-insensitive [root@test3 ~]# grep-nic ' Travel ' Travel.doc 4

2 Search for collection characters using []

[] denotes one of these characters, for example [Ade] represents a or D or E

Find Ting Hing Ning

[Root@test3 ~]# Grep-ni "[thn]ing" Travel.doc

6:meet various people, experience things, which make your world bigger.

7:you just clean up your entire mind and your can get the true meaning of travel.

prefix in [] with the ^ symbol to represent characters other than characters in [].

For example, search for a line that contains no G before OO. Use ' [^g]oo ' as a search string

[Root@test3 ~]# Grep-ni "[^g]oo" Travel.doc

8:foo

[] can be expressed in scope, such as [A-z] for lowercase letters, [0-9] for 0~9 digits, [A-z] for uppercase letters. [A-za-z0-9] represents all numbers and English characters. Of course, you can also match ^ to exclude characters.

[Root@test3 ~]# Grep-ni "[1-9]" Travel.doc

9:2020 12 31

3, beginning and end of line character ^$

^ Represents the beginning of a line, and $ indicates the end of the line (not the character, the position) so ' ^$ ' means a blank line, because only

The beginning and end of a line.

here ^ with [] inside used ^ meaning is different. It means that the string behind the ^ is the beginning of the row.

Like searching for travel at the beginning of the line

[Root@test3 ~]# Grep-ni "^travel" Travel.doc

1:travel is a good way to refresh and broaden our horizon.

4, search for a line that starts with a lowercase letter

[Root@test3 ~]# grep-n "^[a-z]" Travel.doc

8:foot

5, search for lines that don't start with English letters

[Root@test3 ~]# grep-n "^[^a-za-z]" Travel.doc

9:2020 12 31

6,$ indicates that the string before it is the end of the row,

such as ' \. ' At the end of a line

The line at the end of the search is.

[Root@test3 ~]# grep-n \.$ travel.doc 1:travel
-a good way to refresh and broaden our horizon.
2:during your travel, can turn off your cellphone and keep to the Internet.
3:you can forget your work, your study or your, and family just the enjoy time.
4:from the moment when your start your journey, all the trifles should is locked at your house.
5:during the travel, you can kill your time slowly to have a good rest, and your can contacts with different people.
6:they May is a window for your to know a different world.
7:meet various people, experience things, which make your world bigger.
8:you just clean up your entire mind and your can get the true meaning of travel.

7, any one character. With repeating character *

In Bash * represents a wildcard character, which is used to represent arbitrary characters, but in a regular expression, he has a different meaning, * indicates that there are 0 or more characters.

For example, oo*, which means that the first o must exist, the second o can have one or more, or not, and therefore represents at least one o.

Point. Represents an arbitrary character that must exist.

G?? D can be used ' G.. d ' says. Good,gxxd,gabd ..... All fit.

A, search for a string of two o above

[Root@test3 ~]# cat a.sh
good
foot
vnhsdooos
[root@test3 ~]# grep-n "ooo*" a.sh
1:good 2:foot
3:vnhsdooos

b, search the line at the beginning and end of G for the string. * Represents 0 or more arbitrary characters

[Root@test3 ~]# cat b.sh
' Open Source is ' a good mechanism to develop programs.
The GD software is a library for drafting programs.
Google is the best tools for search keyword.
Goooooogle yes!
Go! Go! Let ' s go.
[Root@test3 ~]# grep-n ' g.*g ' b.sh
1: ' Open Source ' is a good mechanism to develop programs.
2:the GD Software is a library for drafting programs.
3:google is the best tools for search keyword.
4:goooooogle yes!
5:go! Go! Let ' s go.

8, limit the range of consecutive repeated characters {}

. * Can only limit 0 or more, and if you want to limit the exact number of characters to repeat, use {range}. The range is a number, separated by the 2,5 means 2~5,

2 means 2, 2, 2 to more

Note that because {} has special meaning in the shell, it is used as a regular expression to escape it.

Searches for a line containing two o strings.

Woody@xiaoc:~/tmp$grep-n ' o\{2\} ' regular_express.txt

1: "OpenSource" isagoodmechanismtodevelopprograms.

2:appleismyfavoritefood.

3:footballgameisnotusefeetonly.

9:oh!thesouptastegood!

18:googleisthebesttoolsforsearchkeyword.

19:goooooogleyes!

Related Article

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.