1. 程式人生 > >RH134-01 配合grep使用正則表達式

RH134-01 配合grep使用正則表達式

rhce

第二章、配合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] tmp]$

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

Aug 8之間是兩個空格。

考題:

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


本文出自 “HCIE_38xx” 博客,謝絕轉載!

RH134-01 配合grep使用正則表達式