基本正则表达式和扩展正则表达式的…

grep常见的基础正则表达式:
1、. :表示出了换行符之外的一个字符
Eg:grep 'h.o' \etc\forlearn
2、* :表示匹配前面n次,其中n可以去任何非负整数
3、\{n,m\} : 表示匹配前面的字符n到m次
4、\{n\} :表示匹配前面的字符n次
5、\{n,\}: 表示匹配前面的字符至少n次
6、^ :表示待匹配字符的开头
Eg:grep '^root' /etc/lianxi
root is just root and not root the main accounts for Linux
root is a user
root is a group name
备注:其实找到的知识三行中的第一个单词,也就是说每行完成搜索后就不在继续往后搜索,每行中找到目标后立即停止

7、$ :表示待匹配字符的结尾,只能找每一行的行尾
8、[] :表示匹配方括号里面指定的字符
9、\ :表示转义字符
10、\<  :界定单词开始
11、\> :界定单词结束
12、\d  :其中d表示digit,其实相当于[0-9],它的使用需要配合-P选项
Eg1:grep [0-9] /etc/lianxi 
137636547812
13、\b :匹配单词的边界
Eg1:grep '\broot\b' /etc/lianxi
root is just root and not root the main accounts for Linux
root is a user
root is a group name
this is root
root
NOTE:精确匹配单词
Eg2:root is just root and not root the main accounts for Linux
root is a user
root is a group name
this is root
root
hehehehehroot   hehehehehroot
NOTE:找出单词root结尾单词
Eg3:root is just root and not root the main accounts for Linux
root is a user
root is a group name
this is root
root
roothihihihihiv   roothihihihihiv
NOTE:找出单词root开始的单词
14、\B  :找出非单词的边界
Eg1:grep ‘rootB\’ /etc/lianxi 
roothihihihihiv   roothihihihihiv
NOTE:寻找以root开头的单词
Eg2:grep '\Broot' /etc/lianxi
hehehehehroot   hehehehehroot
NOTE:寻找以root结尾的单词
15、\w
16、\W
17、其他:\n表示换行;\r表示回车;\t表示制表;\f表示换页;\s表示任何空白字符;\S表示任意非空白字符

扩展的egrep的基础使用:

1、?:匹配前面0次或者1次
2、+ :匹配前面1次或者以上
3、| :表示或
4、():需要与|联合使用

学习通配符
1、*: 代表0个或者更多个字符
2、?: 代表任意一个字符
3、{}: 匹配括号里面内包含以逗号隔开的字符
NOTE:{}可以使用嵌套
4:^或者!代表取反
实例练习
Eg1:寻找good
grep 'good' /etc/testdir/testgrep.txt
good morning teacher
Eg2:寻找good并且不分大小写
grep -i 'good' /etc/testdir/testgrep.txt
good morning teacher
thet teast Good
NOTE:
-i, --ignore-case
              Ignore case distinctions in both the PATTERN and the input files.
Eg3:统计不含good的函数
grep -ivc 'good' /etc/testdir/testgrep.txt
14
NOTE:
-i, --ignore-case
              Ignore case distinctions in both the PATTERN and the input files.
-v, --invert-match
              Invert the sense of matching, to select non-matching lines.  (-v is specified by POSIX.)
-c, --count
              Suppress normal output; instead print  a  count  of  matching  lines  for  each  input  file. 
 
Eg4:正则表达式与grep的使用
1)搜索以good开头的行
grep '^good' /etc/testdir/testgrep.txt
good morning teacher
2)搜索以good结尾的行
grep 'Good$' /etc/testdir/testgrep.txt
thet teast Good
3)使用^与$的组合
grep '^$' /etc/testdir/testgrep.txt #搜索空行,其结果会显示两个空行
grep -c '^$' /etc/testdir/testgrep.txt #显示空白行的个数
4)使用方括号
grep '[Gg]ood' /etc/testdir/testgrep.txt
good morning teacher
thet teast Good
5)在方括号中使用反选^
grep '[^Gg]ood' /etc/testdir/testgrep.txt
what a delicious food
wrong word gooood
6)使用点.
grep 'g..d' /etc/testdir/testgrep.txt
good morning teacher
gold sunshine looks beautiful
you fell gald
wrong word gl0d
wrong word gl2d
wrong word gl3d
grep '[Gg]..d' /etc/testdir/testgrep.txt
[root@iz79m5wcsxarkzz ~]# grep '[Gg]..d' /etc/testdir/testgrep.txt
good morning teacher
gold sunshine looks beautiful
thet teast Good
you fell gald
wrong word gl0d
wrong word gl2d
wrong word gl3d
grep '[Gg][lo][^\n]d' /etc/testdir/testgrep.txt
good morning teacher
gold sunshine looks beautiful
thet teast Good
wrong word gl0d
wrong word gl2d
wrong word gl3d
7)精确匹配使用\< \>
grep 'gold' /etc/testdir/testgrep.txt
gold sunshine looks beautiful
golden time files
grep '\' /etc/testdir/testgrep.txt
gold sunshine looks beautiful
8)使用*号
grep '\
good morning teacher
god bless me
wrong word gooood
php have a gd module
9)使用.*
grep '\
good morning teacher
gold sunshine looks beautiful
golden time files
god bless me
you fell gald
wrong word gooood
wrong word gl0d
wrong word gl2d
wrong word gl3d
php have a gd module
10)使用-
grep 'gl[0-9]d' /etc/testdir/testgrep.txt
grep 'gl[0-9]d' /etc/testdir/testgrep.txt
wrong word gl2d
wrong word gl3d
11)使用\,表示转义字符
grep 'www.helloworld.com' /etc/testdir/testgrep.txt
www.helloworld.com
www@helloworld@com
grep 'www\.helloworld\.com' /etc/testdir/testgrep.txt
www.helloworld.com
grep 'www\@helloworld\@com' /etc/testdir/testgrep.txt
www@helloworld@com
12)使用\{ ,\}
grep 'go\{2,\}d' /etc/testdir/testgrep.txt
wrong word gooood
13) 常见的特殊字符
[:alnum:]:文字数字符
[:alpha:]:文字字符
[:digit:]:数字
[:graph:]:非空字符(除了空格之外、控制字符)
[:print:]:非空字符(包含空格)
[:lower:]:小写字母
[:upper:]:大写字母
[:space:]:空格、新航、制表符
[:xdigit:]:16进制数字(0-9,A-F,a-f)
[:punct:]:标点符号
[:cntrl:]:控制字符
grep '^[[:upper:]]' /etc/testdir/testgrep.txt
Upper case
14)使用egrep
使用扩展正则表示大搜索以g开头后面至少一个o的字母的行
egrep 'go+d' /etc/testdir/testgrep.txt
good morning teacher
god bless me
wrong word gooood
NOTE:因为在扩展正则表达式中+表示匹配前面至少一次
相当于正则表达式中的grep 'go\{1,\}' /etc/testdir/testgrep.txt得出的结果是一样的
使用扩展正则表示g和d之间只有0个或者1个o的单词的行
egrep 'go?d' /etc/testdir/testgrep.txt
god bless me
php have a gd module
与基本正则表达式greo 'go\{0,1\}d' /etc/testdir/testgrep.txt结果一样
使用扩展正则寻找含有gold和glad的行
egrep '(gold|glad)' /etc/testdir/testgrep.txt #也可以把括号去掉
gold sunshine looks beautiful
golden time files
egrep '(g(o|l)(l|a)d)' /etc/testdir/testgrep.txt #扩展正则表达式的另外一种方式

以下是练习的脚本 /etc/testdir/testgrep.txt

-----TEXT BEIN----
  1 good morning teacher
  2 hello world is a script
  3 gold sunshine looks beautiful
  4 god bless me
  5 what a delicious food
  6 thet teast Good
  7 you fell gald
  8 wrong word gooood
  9 wrong word gl0d
 10 wrong word gl2d
 11 wrong word gl3d
 12 www.helloworld.com
 13 www@helloworld@com
 14 Upper case
 15 100% means pure
 16 php have a gd module
-----TEXT END----















你可能感兴趣的:(运维学习笔记)