linux grep command using regular expressions

 

 

Most common application of regular expressions is awk sed grep command

 

[root@MongoDB ~]# cat mike.log 
I am mike!
I like linux.

I like play football, tennis and reading.
my qq is 123456789!
my blog is https://i.cnblogs.com
my blog is i.miek.com
I am miek.
not MIEK
NOT MIKE

 

 

 

 

 

Regular basis Character Description

1. ^ word to word matches the beginning of the content vi / vim editor ^ represents the beginning of a line

2.word $ matches the end of content-word ending in vi / vim editor $ representatives row

3. ^ $ express empty line

 

a. m filtered off line to the beginning of the

 

[root@MongoDB ~]# grep "^m" mike.log 
my qq is 123456789!
my blog is https://i.cnblogs.com
my blog is i.miek.com

b. filtered off line to the end of the m

 

[root@MongoDB ~]# grep "m$" mike.log 
my blog is https://i.cnblogs.com
my blog is i.miek.com

 

c. Line matching filter blank lines

grep -n line numbers match

 

[root@MongoDB ~]# grep -n '^$' mike.log 
3:
11:

 

 

 

grep -v exclude contents of the specified field is not displayed

Filtered off line without the blank lines, the other row print

 

[root@MongoDB ~]# grep -vn "^$" mike.log 
1:I am mike!
2:I like linux.
4:I like play football, tennis and reading.
5:my qq is 123456789!
6:my blog is https://i.cnblogs.com
7:my blog is i.miek.com
8:I am miek.
9:not MIEK
10:NOT MIKE

 

 

 

4. Representatives and can only represent any one character.

5. \ Escape

0 * 6. Repeat one or more of the preceding character e.g. no match o * o, o a o or more

7. * Matches all characters ^. * To begin with any number of characters. * $ To the end of any number of characters

 

a. matches any single character.

Because there is no third row of characters not to match

 

[root@MongoDB ~]# grep -n "." mike.log 
1:I am mike!
2:I like linux.
4:I like play football, tennis and reading.
5:my qq is 123456789!
6:my blog is https://i.cnblogs.com
7:my blog is i.miek.com
8:I am miek.
9:not MIEK
10:NOT MIKE

 

 

.*

 

[root@MongoDB ~]# grep -n ".*" mike.log 
1:I am mike!
2:I like linux.
3:
4:I like play football, tennis and reading.
5:my qq is 123456789!
6:my blog is https://i.cnblogs.com
7:my blog is i.miek.com
8:I am miek.
9:not MIEK
10:NOT MIKE
11:

 

 

 

 Matches any character mi .. ..

root@MongoDB ~]# grep -n "mi.." mike.log 
1:I am mike!
7:my blog is i.miek.com
8:I am miek.

 

grep -i case-insensitive

[root@MongoDB ~]# grep -ni "mi.." mike.log 
1:I am mike!
7:my blog is i.miek.com
8:I am miek.
9:not MIEK
10:NOT MIKE

 

Matches. Ending need to escape

[root@MongoDB ~]# grep -n "\.$" mike.log 
2:I like linux.
4:I like play football, tennis and reading.
8:I am miek.

 

-o display only content that matches

[root @ mongodb ~] # grep -no " mi .. " mike.log 
 1 : mike
 7 : miek
 8 : miek

 

 

8. [abc] matches the character string in any one set [a-zA-Z], [0-9]

After the contents of any 9. [^ abc] ^ does not contain a matching character

In parentheses is the negation ^

 

0-9 matching line that matches the line numbers

[root@MongoDB ~]# grep  "[0-9]" mike.log 
my qq is 123456789!

 

Guess you like

Origin www.cnblogs.com/mingerlcm/p/10930900.html