RH134-01 配合grep使用正则表达式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RH134-01 配合grep使用正则表达式相关的知识,希望对你有一定的参考价值。

第二章、配合grep使用正则表达式

 

2.1 正则表达式基础

         介绍shell中的常用正则表达式

        

 

 

         ^   以什么开头             ^#

         $   以什么结尾              y$

         .   匹配任意一个字符

         .*匹配0个或若干个字符

         h*匹配0h或若干个h   

         h+匹配1个或更多个h

         h?匹配0个或1h

         h{2}  匹配 hh (两个hh)

         [abc]匹配abc

         [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

上课笔记

22

[[email protected] tmp]$ cat cats.txt

t

catdog

cat2dog

catanddog

dogcat

ccat

catdogcccc

c123t

c45678t

Cat

cAt

catdogDogCAT

#this is a cat

;this is a dog

 

[[email protected] tmp]$

[[email protected] tmp]$

[[email protected] tmp]$ grep"cat" /tmp/cats.txt

 

 

 

 

 

[[email protected] tmp]$ grep"cat" /tmp/cats.txt

catdog

cat2dog

catanddog

dogcat

ccat

catdogcccc

catdogDogCAT

#this is a cat

[[email protected] tmp]$ grep -i"cat" /tmp/cats.txt

catdog

cat2dog

catanddog

dogcat

ccat

catdogcccc

Cat

cAt

catdogDogCAT

#this is a cat

[[email protected] tmp]$

 

 

[[email protected] tmp]$ grep ^cat/tmp/cats.txt

catdog

cat2dog

catanddog

catdogcccc

catdogDogCAT

[[email protected] tmp]$

 

 

可以对要找的内容加双 引号,也可以不加

 

[[email protected] tmp]$ grep ^cat /tmp/cats.txt

catdog

cat2dog

catanddog

catdogcccc

catdogDogCAT

[[email protected] tmp]$ ^C

[[email protected] tmp]$ grep"^cat" /tmp/cats.txt

catdog

cat2dog

catanddog

catdogcccc

catdogDogCAT

[[email protected] tmp]$

 

 

 

[[email protected] tmp]$ grep dog$/tmp/cats.txt

catdog

cat2dog

catanddog

;this is a dog

[[email protected] tmp]$ grep ^catdog$/tmp/cats.txt

catdog

[[email protected] tmp]$

 

[[email protected] tmp]$ grep ^cat.*dog$/tmp/cats.txt

catdog

cat2dog

catanddog

[[email protected] tmp]$

.*0个或 若干个其他字符

 

 

 

[[email protected] tmp]$ grep ^cat.dog$/tmp/cats.txt

cat2dog

[[email protected] tmp]$

.表示一个字符

 

 

 

 

[[email protected] tmp]$ grep -E^cat.{3}dog$ /tmp/cats.txt

catanddog

[[email protected] tmp]$ grep ^cat...dog$/tmp/cats.txt

catanddog

[[email protected] tmp]$

以上两种相同,前者显得更专业

 

 

 

[[email protected] tmp]$  grep ^c[0-9]*t$ /tmp/cats.txt

c123t

c45678t

ct也可以匹配

 

 

$ grep -E^"[#;]" /tmp/cats.txt # ;开头的

 

[[email protected] tmp]$ grep -E^"[#;]" /tmp/cats.txt

#this is a cat

;this is a dog

 

 

 

[[email protected] tmp]$ ifconfig | grep[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}

对输出结果进行查找

 

练习:

先下载 下来

[[email protected] 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]

 

[[email protected]lhost tmp]$

 

grep -E "Aug  8 1[34]" /tmp/door.log 

Aug 8之间是两个空格。

 

考题:

Grep  “UUID” /etc/fstab  > /tmp/find.txt


本文出自 “HCIE_38xx” 博客,谢绝转载!

以上是关于RH134-01 配合grep使用正则表达式的主要内容,如果未能解决你的问题,请参考以下文章

grep及扩展正则表达式

linux学习-grep使用正则表达式示例

Linux文本处理三剑客之grep及正则表达式

正则表达式与grep

grep 正则表达式的使用方法

grep与正则表达式