Linux Learning Note: Shell Foundation regular expressions

Source: Internet
Author: User

Commands frequently used in the shell for regular expressions include grep, sed, awk

The contents of the file used for testing

[email protected] ~]# VI testfile

Current Network Status

Last Updated:mon Feb 10:55:06 CST 2016

Updated every seconds

Nagios Core 4.1.1-www.nagios.org

Logged in as Admin


I like Google

It ' s Google, not gooooogle;

Some special symbols in the basic regular expressions

1) ^word represents a line beginning with Word word

[email protected] ~]# grep ^i testfile

I like Google

It ' s Google, not gooooogle;

2) word$ represents the line ending with Word word

[[email protected] ~]# grep S $ testfile

Current Network Status

Updated every seconds


3). The dot number represents an arbitrary character (including spaces)

[email protected] ~]# grep t.g testfile

It ' s Google, not GOoooogle;

4) \ Slash represents the escape character, and all symbols with special meanings are reversed to their own characters (including \ itself)

[[email protected] ~]# grep "\." Testfile

Nagios Core 4. 1. 1-www. Nagios. Org

5) * represents any number of occurrences of the previous character

[[email protected] ~]# grep "O*g" Testfile

NaGiOS Core 4.1.1-www.nagios.org

Logged in as admin

I like Google

It ' s Google, not gooooogle;

In this example, the first line only hits G, because o can occur any number of times, including 0

6). * Matches all characters, including blank lines.

[[email protected] ~]# grep ". *" Testfile

Current Network Status

Last Updated:mon Feb 10:55:06 CST 2016

Updated every seconds

Nagios Core 4.1.1-www.nagios.org

Logged in as Admin

I like Google

It ' s Google, not gooooogle;

7) [] matches any one of the characters in the brackets

[[email protected] ~]# grep "[NO]" Testfile

Current Network Status

NAgios Core 4.1.1-www.nagios.org

It ' s Google, not GoooooGLE;

8) [^chars] CHARS can be multiple letters or ranges that do not match any of the characters in the range. Note that this is a letter, not a word

[ [email protected] ~]# grep "[^g]oo" Testfile

I like google

9) {N,m} limits the number of occurrences of the previous character. Need to add \ Escape when using

[[email protected] ~]# grep "o\{2,3\}" Testfile #当仅使用时, although the limit was 2 to 3 times, but all came out .

I like Gooogle

Not gooooooogle

It ' s Google, not gooooogle;

[[email protected] ~]# grep "Go\{2,3\}gle" Testfile #当用其他字母夹着来用, it is possible to repeat the number of occurrences

I like Gooogle

There are also {n,}, and {n}, which represent at least n times, which represent the exact number of occurrences


Linux Learning Note: Shell Foundation regular expressions

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.