标签:rhce
第二章、配合grep使用正则表达式
2.1 正则表达式基础
介绍shell中的常用正则表达式
^ 以什么开头 ^#
$ 以什么结尾 y$
. 匹配任意一个字符
.*匹配0个或若干个字符
h*匹配0个h或若干个h
h+匹配1个或更多个h
h?匹配0个或1个h
h{2} 匹配 hh (两个hh)
[abc]匹配a或b或c
[a-z]匹配所有的小写字符
[A-Z]匹配大写字母
[a-Z]匹配所有字符
[0-9]匹配所有数字
练习:匹配 IP地址的格式,但无需判断IP是否合理。格式要求满足 以 "."分割是四组数字,每组数字可以1~3位数
0.0.0.0- 255.255.255.255
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}
2.2、使用grep匹配数据
使用grep和正则表达式过滤文件内容和需要的日志内容
练习:建立一个文本/tmp/cats.txt,文本内容如下
cat
caaat
catdog
cat2dog
catanddog
dogcat
ccat
catdogcccc
c123t
c45678t
Cat
cAt
catdogDogCAT
#this is a cat
;this is a dog
$ grep "cat"/tmp/cats.txt
$ grep -i "cat"/tmp/cats.txt 忽略大小写
$ grep ^cat /tmp/cats.txt
$ grep dog$ /tmp/cats.txt
$ grep ^catdog$ /tmp/cats.txt
$ grep ^cat.*dog$ /tmp/cats.txt
$ grep ^cat.dog$ /tmp/cats.txt
$ grep ^cat...dog$ /tmp/cats.txt
$ grep -E^cat.{3}dog$ /tmp/cats.txt 中间3个任意字符
$ grep ^c[0-9]*t$/tmp/cats.txt [0-9]* 匹配0个或若干个数字
$ grep -E^"[#;]" /tmp/cats.txt 以# ;开头的
$ grep -e ^"#"-e ^";" /tmp/cats.txt 作用同上,-e 可以通过指定多个表达式
练习1:过去日志,把 August 8 sometime between 1:00pm and 3:00pm 时间段的日志找到
http://classroom.example.com/pub/materials/awesome_logs/door.log
$ grep "Aug 8 1[34]" door.log
=========================================================================
2.2
上课笔记
2.2
[student@localhost tmp]$ cat cats.txt
t
catdog
cat2dog
catanddog
dogcat
ccat
catdogcccc
c123t
c45678t
Cat
cAt
catdogDogCAT
#this is a cat
;this is a dog
[student@localhost tmp]$
[student@localhost tmp]$
[student@localhost tmp]$ grep"cat" /tmp/cats.txt
[student@localhost tmp]$ grep"cat" /tmp/cats.txt
catdog
cat2dog
catanddog
dogcat
ccat
catdogcccc
catdogDogCAT
#this is a cat
[student@localhost tmp]$ grep -i"cat" /tmp/cats.txt
catdog
cat2dog
catanddog
dogcat
ccat
catdogcccc
Cat
cAt
catdogDogCAT
#this is a cat
[student@localhost tmp]$
[student@localhost tmp]$ grep ^cat/tmp/cats.txt
catdog
cat2dog
catanddog
catdogcccc
catdogDogCAT
[student@localhost tmp]$
可以对要找的内容加双 引号,也可以不加
[student@localhost tmp]$ grep ^cat /tmp/cats.txt
catdog
cat2dog
catanddog
catdogcccc
catdogDogCAT
[student@localhost tmp]$ ^C
[student@localhost tmp]$ grep"^cat" /tmp/cats.txt
catdog
cat2dog
catanddog
catdogcccc
catdogDogCAT
[student@localhost tmp]$
[student@localhost tmp]$ grep dog$/tmp/cats.txt
catdog
cat2dog
catanddog
;this is a dog
[student@localhost tmp]$ grep ^catdog$/tmp/cats.txt
catdog
[student@localhost tmp]$
[student@localhost tmp]$ grep ^cat.*dog$/tmp/cats.txt
catdog
cat2dog
catanddog
[student@localhost tmp]$
.*0个或 若干个其他字符
[student@localhost tmp]$ grep ^cat.dog$/tmp/cats.txt
cat2dog
[student@localhost tmp]$
.表示一个字符
[student@localhost tmp]$ grep -E^cat.{3}dog$ /tmp/cats.txt
catanddog
[student@localhost tmp]$ grep ^cat...dog$/tmp/cats.txt
catanddog
[student@localhost tmp]$
以上两种相同,前者显得更专业
[student@localhost tmp]$ grep ^c[0-9]*t$ /tmp/cats.txt
c123t
c45678t
ct也可以匹配
$ grep -E^"[#;]" /tmp/cats.txt 以# ;开头的
[student@localhost tmp]$ grep -E^"[#;]" /tmp/cats.txt
#this is a cat
;this is a dog
[student@localhost tmp]$ ifconfig | grep[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}
对输出结果进行查找
练习:
先下载 下来
[student@localhost tmp]$ wget -O/tmp/door.log http://classroom.example.co
m/pub/materials/awesome_logs/door.log
--2017-06-25 12:22:01-- http://classroom.example.com/pub/materials/awesome_logs/door.log
Resolving classroom.example.com(classroom.example.com)... 172.25.254.254
Connecting to classroom.example.com(classroom.example.com)|172.25.254.254|:80... connected.
HTTP request sent, awaiting response... 200OK
Length: 58722 (57K) [text/plain]
Saving to: ‘/tmp/door.log’
100%[======================================>]58,722 --.-K/s in 0.001s
2017-06-25 12:22:02 (43.6 MB/s) -‘/tmp/door.log’ saved [58722/58722]
[student@localhost tmp]$
grep -E "Aug 8 1[34]" /tmp/door.log
Aug 8之间是两个空格。
考题:
Grep “UUID” /etc/fstab > /tmp/find.txt
本文出自 “HCIE_38xx” 博客,谢绝转载!
标签:rhce
原文地址:http://hciewd.blog.51cto.com/3272922/1941664