First, the basic regular environment preparation
[[email protected] oldboy]# cat /oldboy/re.txtI am oldboy teacher!I teach linux.I like badminton ball ,billiard ball and chinese chess!my blog is http://oldboy.blog.51cto.comour size is http://blog.oldboyedu.commy qq is 49000448not 4900000448.my god ,i am not oldbey,but OLDBOY!
1. ^ ^m .... Line starting with
[[email protected] oldboy]# grep ' ^m ' re.txt
My blog is http://oldboy.blog.51cto.com
My QQ is 49000448
My God, I am not oldbey,but oldboy!
2. $ m$ means to .... End of Line
[Email protected] oldboy]# cat-a re.txt
I am Oldboy teacher!$
I Teach linux.$
$
I like badminton ball, billiard ball and Chinese chess!$
My blog is http://oldboy.blog.51cto.com $ #注意此处结尾是空格不是m
$
Our size is http://blog.oldboyedu.com $ #注意此处结尾是空格不是m
$
My QQ is 49000448$
$
Not 4900000448.$
My God, I am not oldbey,but oldboy!$
#删除结尾的空格
[Email protected] oldboy]# vim Re.txt
I am Oldboy teacher!
I Teach Linux.
I like badminton ball, billiard ball and Chinese chess!
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
My QQ is 49000448
Not 4900000448.
My God, I am not oldbey,but oldboy!
#检查是否修改成功
[Email protected] oldboy]# cat-a re.txt
I am Oldboy teacher!$
I Teach linux.$
$
I like badminton ball, billiard ball and Chinese chess!$
My blog is http://oldboy.blog.51cto.com$
$
Our size is http://blog.oldboyedu.com$
$
My QQ is 49000448$
$
Not 4900000448.$
My God, I am not oldbey,but oldboy!$
[Email protected] oldboy]#
[[email protected] oldboy]# grep ' m$ ' re.txt
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
Cat-a #显示出文件中所有的符号 $ The end of this line
[[email protected] oldboy]# grep ' re.txt
I am Oldboy teacher!
I Teach Linux.
I like badminton ball, billiard ball and Chinese chess!
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
My QQ is 49000448
Not 4900000448.
My God, I am not oldbey,but oldboy!
3, ^$ empty line There is nothing in this line
[Email protected] oldboy]# grep-n ' ^$ ' re.txt
3:
6:
8:
10:
Exercise: Troubleshoot blank lines in a file
[Email protected] oldboy]# grep-v ' ^$ ' re.txt
I am Oldboy teacher!
I Teach Linux.
I like badminton ball, billiard ball and Chinese chess!
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
My QQ is 49000448
Not 4900000448.
My God, I am not oldbey,but oldboy!
4,. Any one character does not match a blank line
#grep-o '. ' Re.txt
#-o shows the execution of grep, and grep finds out what each time
Grep-o '. ' Re.txt
#-o shows the execution of grep, and grep finds out what each time
Each row in the results of the #-o indicates what grep found every time
[[email protected] oldboy]# grep ' 0 ' re.txt
My QQ is 49000448
Not 4900000448.
[Email protected] oldboy]# grep-o ' 0 ' re.txt
0
0
0
0
0
0
0
0
[[email protected] oldboy]# grep ' xx ' Re.txt
My QQ is 49000448
Not 4900000448.
[Email protected] oldboy]# grep-o ' xx ' Re.txt
00
00
00
Description: If the matching content is in more than one row, grep will match from left to right to the last, and the more
Tip: A summary of the special meanings of points (.):
1. Current directory
2, make the file effective equivalent to source
3, the beginning of hidden files
4, any one character
5. Crowbar escape character Remove symbol special meaning take off your vest and return to your original shape.
Shows the lines in the file that end in.
[[email protected] oldboy]# grep '. $ ' re.txt
I Teach Linux.
Not 4900000448.
Crowbar Series Escape Character series
\ ====== Enter.
6, * The previous character appears 0 times or more than 0 times in a row
[[email protected] oldboy]# grep ' 0* ' re.txt
I am Oldboy teacher!
I Teach Linux.
I like badminton ball, billiard ball and Chinese chess!
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
My QQ is 49000448
Not 4900000448.
My God, I am not oldbey,but oldboy!
[Email protected] oldboy]#
[[email protected] oldboy]# grep ' 0* ' Re.txt-o
000
00000
#连续出现
#正则表示连续出现的时候, will match as much as possible (eat) more symbolic greed
[[email protected] oldboy]# #0 * Indicates 0 consecutive occurrences 0 times
[[email protected] oldboy]# #0 * Indicates 0 consecutive occurrences 1 times and more than 1 times
[Email protected] oldboy]# #0
[Email protected] oldboy]# #000
[Email protected] oldboy]# #0000000
[[email protected] oldboy]# #0 * Indicates 0 consecutive occurrences 0 times
[[Email protected] oldboy]# # ' 0* ' only appears 0 times ====== '
[Email protected] oldboy]# #会把整个文件的内容都显示出来
[[email protected] oldboy]# grep ' re.txt
I am Oldboy teacher!
I Teach Linux.
I like badminton ball, billiard ball and Chinese chess!
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
My QQ is 49000448
Not 4900000448.
My God, I am not oldbey,but oldboy!
Summary of the regular *:
1]] greedy sex
2]] Continuous appearance
7. * All symbols
#在正则中表示连续出现 means all greed.
Find lines in a file that begin with the letter M and end with M
[[email protected] oldboy]# grep ' ^m ' re.txt
My blog is http://oldboy.blog.51cto.com
My QQ is 49000448
My God, I am not oldbey,but oldboy!
[[email protected] oldboy]# grep ' ^m ' re.txt |grep ' m$ '
My blog is http://oldboy.blog.51cto.com
[[email protected] oldboy]# grep ' ^m.*m$ ' re.txt
My blog is http://oldboy.blog.51cto.com
[[email protected] oldboy]# grep ' ^.*m$ ' re.txt
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
[[email protected] oldboy]# grep ' m$ ' re.txt
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
[[email protected] ~]# grep '. *m ' Re.txt
8, [] [ABC] denotes a whole, a or B or c any one character
[[email protected] oldboy]# grep ' [ABC] ' Re.txt
I am Oldboy teacher!
I Teach Linux.
I like badminton ball, billiard ball and Chinese chess!
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
My God, I am not oldbey,but oldboy!
grep ' [ABC] ' Re.txt-o
Where ===> come from, where to use is-
[[email protected] oldboy]# grep ' [A-z] ' Re.txt
I am Oldboy teacher!
I Teach Linux.
I like badminton ball, billiard ball and Chinese chess!
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
My QQ is 49000448
Not 4900000448.
My God, I am not oldbey,but oldboy!
[[email protected] oldboy]# grep ' [A-z] ' Re.txt
I am Oldboy teacher!
I Teach Linux.
I like badminton ball, billiard ball and Chinese chess!
My God, I am not oldbey,but oldboy!
[[email protected] oldboy]# grep ' [a-za-z] ' re.txt
I am Oldboy teacher!
I Teach Linux.
I like badminton ball, billiard ball and Chinese chess!
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
My QQ is 49000448
Not 4900000448.
My God, I am not oldbey,but oldboy!
#找出文件中以m或n开头的行
[[email protected] oldboy]# #第1个里程碑-M or N
[[email protected] oldboy]# grep ' [mn] ' re.txt
I am Oldboy teacher!
I Teach Linux.
I like badminton ball, billiard ball and Chinese chess!
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
My QQ is 49000448
Not 4900000448.
My God, I am not oldbey,but oldboy!
[[email protected] oldboy]# #第2个里程碑-lines beginning with M or n
[[email protected] oldboy]# grep ' ^[mn] ' re.txt
My blog is http://oldboy.blog.51cto.com
My QQ is 49000448
Not 4900000448.
My God, I am not oldbey,but oldboy!
grep ' [A-z] ' Re.txt
grep ' [A-z] ' Re.txt
grep ' [0-9] ' re.txt
9, [^] exclude [^ABC]
[[email protected] ~]# grep ' [^abc] '/oldboy/re.txt
I am Oldboy teacher!
I Teach Linux.
I like badminton ball, billiard ball and Chinese chess!
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
My QQ is 49000448
Not 4900000448.
My God, I am not oldbey,but oldboy!
|||||||||||||||||| 000000000
Two, extended regular 1, + the previous character appears 1 or more times in a row
[Email protected] oldboy]# egrep ' 0+ ' re.txt
My QQ is 49000448
Not 4900000448.
[Email protected] oldboy]# egrep ' 0 ' re.txt
My QQ is 49000448
Not 4900000448.
[Email protected] oldboy]# egrep ' 0+ ' Re.txt-o
000
00000
#+ can turn successive characters into a whole.
[Email protected] oldboy]# egrep ' 0 ' re.txt-o
0
0
0
0
0
0
0
0
[Email protected] oldboy]# #连续出现
Remove successive lowercase letters from a file
[Email protected] oldboy]# #取出文件中连续出现的小写字母
[[email protected] oldboy]# egrep ' small Letter + ' Re.txt
[[email protected] oldboy]# egrep ' [a-z]+ ' Re.txt
I am Oldboy teacher!
I Teach Linux.
I like badminton ball, billiard ball and Chinese chess!
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
My QQ is 49000448
Not 4900000448.
My God, I am not oldbey,but oldboy!
[[email protected] oldboy]# egrep ' [a-z]+ ' Re.txt-o
Am
Oldboy
Teacher
Teach
Linux
Like
Badminton
Ball
Billiard
Ball
and
Chinese
Chess
My
Blog
Is
http
Oldboy
Blog
Cto
Com
Our
Size
Is
http
Blog
oldboyedu
Com
My
Qq
Is
Not
My
God
I
Am
Not
Oldbey
But
Summary:
1.+ 1 or more consecutive occurrences
You can turn successive characters into a whole, one at a time (-O)
+ General and [] mates
#1. Find out the Rules
#2. Chinese and English symbols
#3. Case sensitivity
[Email protected] oldboy]# egrep ' ^[0-9]+$ ' id.txt
440304199604012792
130528197108126121
342923198310042132
330900199806382320
654126197703092303
131127197105115662
[Email protected] oldboy]# egrep ' ^[0-9]+x$ ' id.txt
61242619860416291X
[Email protected] oldboy]# egrep ' ^[0-9]+[0-9x]$ ' id.txt
440304199604012792
130528197108126121
342923198310042132
61242619860416291X
330900199806382320
654126197703092303
131127197105115662
2, | Or
[[email protected] oldboy]# egrep ' Linux or Oldboy ' Re.txt
[Email protected] oldboy]# egrep ' Linux|oldboy ' re.txt
I am Oldboy teacher!
I Teach Linux.
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
3, () becomes a whole reverse reference back to reference
#先乘除后加减, with parentheses in the first parentheses
[Email protected] oldboy]# egrep ' Oldbo|ey ' re.txt
I am Oldboy teacher!
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
My God, I am not oldbey,but oldboy!
[Email protected] oldboy]# egrep ' oldb (o|e) y ' re.txt
I am Oldboy teacher!
My blog is http://oldboy.blog.51cto.com
Our size is http://blog.oldboyedu.com
My God, I am not oldbey,but oldboy!
#反向引用 Back to reference sed
# #先通过 () protect what you want and then use
Echo 123456|xxxxxxx
<123456>
[Email protected] oldboy]# echo 123456
123456
[[email protected] oldboy]# echo 123456|sed-r ' s# (. *) #\1#g '
123456
[[email protected] oldboy]# echo 123456|sed-r ' s# (. *) #<\1> #g '
<123456>
Echo 123456
34
[Email protected] oldboy]# echo 123456
123456
[Email protected] oldboy]# echo 123456|sed-r ' s#12 (56#\1#g ')
34
[[email protected] oldboy]# echo 123456|sed-r ' s# (1) 2 (5 (6) #\2#g '
34
[Email protected] oldboy]# echo 123456|sed-r ' s# (.). (..).. #\2#g '
34
Summary:
1. () parenthesis [] bracket {} Curly brace curly brace
2. Back reference (SED)
4, {} curly Braces
0{n,m} The previous character appears consecutively at least n times, up to M times >=n && <=m
0{n} The previous character appears consecutively n times ==n
0{n,} The previous character appears consecutively at least n times >=n
0{,m} The previous character has a continuous occurrence of up to M times <=m
[[email protected] oldboy]# egrep ' 0{1,4} ' re.txt
My QQ is 49000448
Not 4900000448.
[[email protected] oldboy]# egrep ' 0{1,4} ' Re.txt-o
000
0000
0
[[email protected] oldboy]# egrep ' 0{2,4} ' re.txt
My QQ is 49000448
Not 4900000448.
[[email protected] oldboy]# egrep ' 0{2,4} ' Re.txt-o
000
0000
#取出文件中连续出现8次到10次的数字
[Email protected] oldboy]# #取出文件中连续出现8次到10次的数字
[[email protected] oldboy]# egrep ' [0-9]{8,10} ' Re.txt
My QQ is 49000448
Not 4900000448.
[Email protected] oldboy]# egrep ' ^[0-9]{17}[0-9x]$ ' id.txt
440304199604012792
130528197108126121
342923198310042132
61242619860416291X
330900199806382320
330900199806382320
654126197703092303
Third, remove the IP address (ifconfig IP) method 1-sed
[Email protected] oldboy]# ifconfig eth0|sed-n ' 2p '
inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
[Email protected] oldboy]# ifconfig eth0|sed-n ' 2p ' |sed ' s#^.*:# #g '
255.255.255.0
[Email protected] oldboy]# ifconfig eth0|sed-n ' 2p ' |sed ' s#^.*addr:# #g '
10.0.0.200 bcast:10.0.0.255 mask:255.255.255.0
[Email protected] oldboy]# ifconfig eth0|sed-n ' 2p ' |sed ' s#^.*addr:# #g ' |sed ' s# bc.*$# #g '
10.0.0.200
#方法2-sed-back to reference
[Email protected] oldboy]# ifconfig eth0|sed-n ' 2p '
inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
[[email protected] oldboy]# ifconfig eth0|sed-n ' 2p ' |sed-r ' S#^.*DR: (. *) Bc.*$#\1#g '
10.0.0.200
#方法3-awk-to specify multiple separators
[Email protected] oldboy]# ifconfig Eth0|awk ' nr==2 '
inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
[Email protected] oldboy]# ifconfig eth0|awk ' nr==2 ' |awk-f "[:]+" ' {Print $4} '
10.0.0.200
[[email protected] oldboy]# ifconfig eth0|awk ' nr==2 ' |egrep ' [:] '
inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
[[email protected] oldboy]# ifconfig eth0|awk ' nr==2 ' |egrep ' [:]+ '
inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
#方法4-awk-' condition {command} '
[Email protected] ~]# ifconfig Eth0|awk ' nr==2 '
inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
[[email protected] ~]# ifconfig eth0|awk ' nr==2 ' |awk ' {print $} '
addr:10.0.0.200
[[email protected] ~]# #awk ' Who to look for {What to do} '
[[email protected] ~]# #awk ' condition {command} '
[[email protected] ~]# ifconfig Eth0|awk ' nr==2{print $} '
addr:10.0.0.200
[[email protected] ~]# #错误 ifconfig eth0|awk-f "[:]+" ' Nr==2{print $} '
[[email protected] ~]# ifconfig eth0|awk-f "[:]+" ' Nr==2{print $4} '
10.0.0.200
#方法5-sed
Sed-nr ' 2S#^.*DR: (. *) BC.*$#\1#GP '
Sed-n ' 2S#INET#OLDBOY#GP '
[Email protected] ~]# ifconfig eth0|sed-n ' 2S#INET#OLDBOY#GP '
oldboy addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
Old boy It education produced-sed command Reverse reference remove NIC IP address
Https://www.processon.com/view/link/59fa9baae4b0f84f8975eefe
#方法6
[[Email protected] ~]# IP a s eth0 |awk ' nr==3 '
inet 10.0.0.200/24 brd 10.0.0.255 scope global eth0
[[Email protected] ~]# IP a s eth0 |awk ' nr==3 ' |awk-f ' [/]+ ' ' {print $} '
10.0.0.200
#方法7
[[Email protected] ~]# IP a s eth0 |sed-n ' 3p '
inet 10.0.0.200/24 brd 10.0.0.255 scope global eth0
[[Email protected] ~]# IP a s eth0 |sed-n ' 3p ' |sed-r ' s#^.* (. *)/# #g '
BRD 10.0.0.255 Scope Global eth0
[[Email protected] ~]# IP a s eth0 |sed-n ' 3p ' |sed-r ' s#^.* ([0-9.] +)/.*$#\1#g '
10.0.0.200
Iv. removing file Permissions # method 1
[[email protected] ~]# stat/etc/hosts |awk-f "[(/]" ' nr==4 '
Access: (0644/-rw-r--r--) Uid: (0/root) Gid: (0/root)
[[email protected] ~]# stat/etc/hosts |awk-f "[(/]" ' Nr==4{print $} '
0644
#方法2-sed
[Email protected] ~]# stat/etc/hosts |sed-n ' 4p '
Access: (0644/-rw-r--r--) Uid: (0/root) Gid: (0/root)
[Email protected] ~]# stat/etc/hosts |sed-n ' 4p ' |sed-r ' s#^.*\ (0.*/# #g '
root\)
[[email protected] ~]# stat/etc/hosts |sed-n ' 4p ' |sed-r ' s#^.*\ (0 (. *)/-# #g '
rw-r--r--) Uid: (0/root) Gid: (0/root)
[[email protected] ~]# stat/etc/hosts |sed-n ' 4p ' |sed-r ' s#^.*\ (0 (. *)/-.*$# #g '
[[email protected] ~]# stat/etc/hosts |sed-n ' 4p ' |sed-r ' s#^.*\ (0 (. *)/-.*$#\1#g '
644
https://www.processon.com/view/link/59fbc9c0e4b0f84f89765231
#方法3
[Email protected] ~]# stat/etc/hosts |awk ' nr==4 ' |sed-r ' s#^.*\ (0|/.*$# #g '
644
#方法4-stat
[Email protected] ~]# stat-c%a/etc/hosts
644
[Email protected] ~]# #命令的结果中 have what you want.
Exercises:
1, take the IP address
2. Take file permissions
Basic Learning -10.4-Regular expression exercises for Linux