grep Regular Expression

Source: Internet
Author: User
Tags grep regular expression

Regular expressions

Use "A string of symbols" to describe data with common attributes (for filtering data)

Regular expressions are made up of regular symbols


Grep's role is to filter out the data you want in a bunch of data.


Format

grep [Options] "Regular Expressions" file list (spaces between multiple files)

grep ' root '/etc/passwd /etc/shadow


Command | grep [option] ' Regular expression ' | grep [option] ' Regular expression '

PS aux | grep ' httpd '


option to process data by default when no options are selected

The default is to output the data that matches the regular expression to the screen


The purpose of the Add option is to change the way grep handles data

Regular expressions must write items

Can consist of ordinary character number symbols ""

such as grep root/etc/passwd

It can also be made up of regular symbols (metacharacters) "


File list space between multiple files

Finish all the lines in the first file before processing all the rows in the next file


Process data from the preceding command, sequentially processing each row of data in the command output


The order in which the grep command is processed

1, in the behavior of processing units

2, the data line-by processing, processing the current line automatically processing the next line, until all the rows are finished processing

3. The default output has rows that match data to regular expressions, to the on-screen

4, do not add the option, the default way to process the data


Options


--color displays the data that matches the regular expression with red

-N output line number in a file that matches a regular expression

-C statistics matches the number of rows with regular expressions

-Q Masking Default output

-I ignores letter case matching

-V output rows that do not match regular expressions

-E Remove escape symbols in regular expressions


Regular expressions consist of regular symbols (meta-characters)


$ Match Line end ^$ Blank line

^ Match the beginning of ^# ^a ^awk with a beginning behind WK followed


. Match any single character except a newline (\ n)

grep ' \.$ ' a.txt display with '. ' The end line needs to be escaped with the symbol ' \ '


* matches the number of occurrences of the preceding character or regular expression

grep ' Go*d ' Abc.txt front is g behind is d,1 0 appears 0 or more times

grep ' Gooo*d ' Test.txt front is goo followed by d,1 0 appears 0 or more times

grep ' go.d ' abc.txt '. ' Display any one character

() to match the regular table XT as a whole

grep ' g\ (oo\) *d ' abc.txt//Two o appears 0 times to multiple times

Grep-e ' G (oo) *d abc.txt '//-e remove escape characters from regular expressions



. * Match All characters


| or grep ' Root\|daemon '/etc/passwd

Grep-e ' Root|daemon '/etc/passwd

Grep-v-E ' ^$|^# '/etc/passwd


[] The number of occurrences in the range match (any one within the matching range) is one

grep ' [139] ' Test.txtxt matches the specified number

Grep-e ' 1|3|9 ' test.txt

grep ' [0-9] ' test. Match all the numbers


grep ' [#_-] ' Test.txt matches #_-rows

grep ' [#-_] ' test.txt matches lines from # to _

grep ' [?! _-] ' test.txt match characters in range


[awk] matches all rows that contain awk

[awk] matches all rows containing AWK

[A-z] matches all lowercase letters

[A-z] matches all uppercase letters

[A-z] matches all uppercase and lowercase letters

[A-za-z] matches all uppercase and lowercase letters

[a-z0-9] matches all numbers and uppercase and lowercase letters


[^a-z] within the scope of the inverse


^[0-9] Lines starting with a number range

^[157] begins with the number 1 with the number 5 beginning with the number 7


^[^A-Z] lines that do not start with any lowercase letters



Sets the number of occurrences of a regular expression that matches the number of occurrences of a regular expression (plus "\" escape)


* Match front of regular expression appears 0 times to multiple >=0

\+ matches the preceding regular expression appears 1 times to multiple >=1

\? Matches the preceding regular expression appears 0 to 1 times <=1


\{n,m\} matches the specified number of occurrences of the preceding regular expression

\{3\} match precedes character or regular expression occurs 2 times = 3

\{3,\} matches the character or regular expression that precedes it at least 2 times >=

\{3,5\} matches the preceding character or regular expression at least 2 occurrences 4 times >=3 and <=5


Cases

grep--color-e ' g {3} ' test.txt 3 0 Occurrences 3 times

grep--color-e ' g0{1}d ' test.txt a 0 appears 1 times

grep--color-e ' [0-9]{3} ' test.txt 0-9 any number that appears 3 times in a row

grep--color-e ' go*d ' test.txt appears 0 times to multiple times

grep--color-e ' go+d ' test.txt appears 1 times to multiple times

grep--color-e ' go?d ' test.txt appears 0 times to 1 times



\< matches the beginning of the word grep--color "\<th" test.txt \ is not an escape symbol must exist

\> match word end grep--color "M\>" test.txt


Cases

Head httpd.conf | Grep-i ' \<th '--color shows the beginning of the \th

Head httpd.conf | Grep-i ' on\> '--color shows the end of



Regular expressions that match the MAC address

00:0c:29:c1:c1:ff


0 1 2 3 4 5 6 7 8 9 A B C D E F

A b c d E F


[0-9a-fa-f] {2}:[0-9a-fa-f]{2}:[0-9a-fa-f]{2}:[0-9a-fa-f]{2}:[0-9a-fa-f]{2}:

[0-9a-fa-f] {2}



Grep-e ' ([0-9a-fa-f]{2}:) {5}[0-9a-fa-f]{2} ' Ifcfg-eth0


This article is from the "Wsyht blog" blog, make sure to keep this source http://wsyht2015.blog.51cto.com/9014030/1786328

grep Regular Expression

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.