Shell Regular Expression

Source: Internet
Author: User
Tags control characters lowercase posix printable characters alphanumeric characters egrep

What is a regular

  A regular is a way to describe a character or string by combining symbols with special meanings, called regular expressions. Or, the regular is the rule used to describe a class of things.

There are regular things in life:

For example we describe: 4 legs

You might think of a four-legged animal or a table, a chair, etc.

Continue description: 4 legs, live

It's just a four-legged animal.

In Linux, wildcard characters are interpreted by the shell, and regular expressions are interpreted by commands, so let's introduce three types of text processing Tools/commands: grep, sed, awk, all of which can be interpreted as regular.

Two grep

Parameters

-N: Show line numbers

-O: Show only matching content

-Q: Silent mode, no output, you have to use $? To determine the success of the execution, that is, there is no filtering to the desired content

-L: If the match succeeds, only the file name is printed, the failure is not printed, usually-rl together, grep-rl ' root '/etc

-A: If the match is successful, the matching row and the subsequent n rows are printed together

-B: If the match succeeds, the matching row and its first n rows are printed together

-C: If the match succeeds, the matching row and its n rows are printed together

-C: If the match succeeds, the number of rows to match is printed

-e: Equals Egrep, extended

-I: Ignore case

-V: Inverse, mismatch
-W: Match word

[[email protected] ~]# cat a.txt root123root asdfroot_123rootssroot 123[[email protected] ~]# grep-i "root" a.txt root123 Root asdfroot_123rootssroot 123[[email protected] ~]# grep-w "root" a.txt root 123

grep type
Grep
Fgrep
Pgrep
Egrep

The regular introduction

^ Beginning of the line
$ End of line
. Any single character other than the line break
* 0 or more of the leading characters
. * All Characters
[] Any character within a group of characters
[^] reverse each character within a character group (does not match each character in a group of characters)
^[^] lines that begin with characters within a non-character group
[A-z] lowercase letter
[A-z] capital letters
[A-z] lowercase and uppercase letters
[0-9] Number
\< words are usually separated by spaces or special characters, and successive strings are treated as words.
\> Word Tail

Extended regular sed plus-r parameter or escape
grep plus-E or egrep or escaped
AWK directly supports but does not contain {n,m}
You can use--posix support
[[email protected] ~]# awk '/ro{1,3}/{print} '/etc/passwd
[[email protected] ~]# awk--posix '/ro{1,3}/{print} '/etc/passwd

Sed-n '/roo\?/p '/etc/passwd
Sed-rn '/roo?/p '/etc/passwd
? A leading character 0 or one
+ Leading character one or more
ABC|DEF ABC or DEF
A (Bc|de) f ABCF or Adef
x\{m\} X appears m times
x\{m,\} X appears m times to multiple times (at least m times)
x\{m,n\} X appears m times to N times

POSIX-defined character classification

[: Alnum:] alphanumeric characters. The
Match range is [a-za-z0-9]
[: Alpha:] alphabetic characters.
The match range is [a-za-z]
[: blank:] Space or tab characters.
The match range is a space and the TAB key
[: Cntrl:] Control characters.
Match control keys such as ^m to press CTRL + V and press ENTER to output
[:d igit:] Numeric characters.
Match all numbers [0-9]
[: graph:] characters that is both printable and visible. (A space is the print-
able, but not visible, while an A is both.)
Match all visible characters without spaces and tab is all the symbols you can see with your eyes on your keyboard in a text document
[: Lower:] lower-case alphabetic characters.
lowercase [A-z]
[:p rint:] Printable characters (characters that is not control characters.)
Match all visible characters including spaces and tab
to all symbols printed on the paper
[:p UNCT:] punctuation characters (characters that is not letter, digits, con-< Br>trol characters, or space characters).
Special input symbol +-=) (*&^%$#@!~ ' |\ "' {}[]:;? />.<,
Note that it does not contain spaces and tab
This collection does not equal ^[a-za-z0-9]
[: space:] space characters (such as space, tab, and FormFeed, to Name a few).

[: Upper:] Upper-case alphabetic characters.
uppercase [A-z]
[: Xdigit:] Characters that is hexadecimal digits.
16 binary number [0-f]

grep job

Shell Regular Expression

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.