新手學習Linux之grep

在馬哥教育學習Linux第二週,這周又學習了一些Linux的基礎知識,通過寫這篇文章對自己的這周所學做一個簡單的梳理和回顧。

grep是一款Unix上的命令行工具,它最初設計開發用於Unix操作系統,但是如今幾乎所有的類Unix(Unix-like)操作系統都在使用。grep是由Unix的創造者之一的Ken Thompson所編寫,第一次出現在是version 4 Unix中 。 grep是英文 globally search a regular expression and print的縮寫,它的作用從名字就能看出,使用正則表達式做全局搜索並打印出所有匹配行。grep和egrep、fgrep構成了grep家族,後兩者是由AWK的聯合創作者Al Aho在1975年編寫。

Linux作爲一款類Unix系統,其使用的也是grep,不過Linux使用的應該是GNU grep,也是GNU版本的grep。

在談到如何使用grep之前我們的先學習下正則表達式。

基本正則表達式元字符

1)字符匹配。

.

 匹配任意單個字符(換行符除外)

[]

匹配範圍內的任意單個字符

[^]

匹配範圍外的任意單個字符

2)匹配次數:限制其前面的字符要出現的次數。

*

匹配前面的字符任意次(0次、1次或者多次),例如 .* 代表的就是任意長度任意字符

\+

匹配前面的字符至少一次

\?

匹配之前的字符0次或者一次

\{m\}

匹配之前的字符m次,m未非負整數,例x\{2\} 匹配x兩次

\{m,n\}

匹配之前的字符至少出現m次,最多出現n次。m和n爲非負整數。

3)位置錨定:限制匹配到的字符在文本中出現的位置。grep搜索是貪婪模式的,只要字符串中有匹配到的字符就打印出來,所以加位置錨定更加精確查找。

^

行首錨定,用在模式的最左側。例: ^a的意思是以a開頭的行。

$

行尾錨定,用在模式的最右側。例:a$的意思是以a結尾的行。^$表示空行。

\<或\b

單詞詞首錨定,用於單詞模式的最左側,限制要匹配的單詞要以什麼開頭。

\>或\b

單詞詞尾錨定,用於單詞模式的最右側,限制要匹配的單詞要以什麼結尾。例:\<this\> 匹配this這個單詞。

4)分組與引用

\(PATTERN\) :將此PATTERN匹配到的字符當做一個不可侵害的整體進行處理。分組括號中的模式匹配到的字符會被正則表達式引擎自動記錄在內部的變量中,這些變量是\1、\2...,\1表示第一組括號中的pattern中匹配到字符串,同理\2表示第二組括號中匹配到的字符串。具體的說就是\n:模式中第幾個左括號以及與之匹配的右括號之間的模式所匹配到的字符串;不是模式本身,而是匹配到的結果。

後向引用:引用前面的括號中的模式所匹配的字符串。舉例說明:

test.txt文件中有兩行字符串分別爲:abtcd fbtce和dbtca rbdca。我要找出找出第一個字符串就可以使用".\(b.c\).*\1."精確的匹配出了。具體操作命令在本文後面給出。

5)POSIX字符類

爲了在不同的國家的字符編碼中保持一致, POSIX(The Portable Operating System Interface)增加了特殊的字符類,常見的有:

[[:alnum:]]

所有字母和數字

[[:alpha:]]

所有字母

[[:digit:]]

所有數字

[[:upper:]]

所有大寫字母

[[:lower:]]

所有小寫的字母

[[:space:]]

空白字符

[[:punct:]]

標點符號

擴展正則表達式與基本正則的主要區別如下:      

基本正則表達式
擴展正則表達式
\++
\??
\{m\}{m}
\{m,n\}{m,n}
\(PATTERN\){m,n}

另外,擴展正則表達式多了一個或者“|”的概念。

介紹完了基本正則表達式,我們就可以來說說grep的具體用法了。前文已經對grep的來歷和作用做了簡單的介紹,現在我們來介紹下grep的基本用法。

命令的基本格式:

grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

常用命令選項:

       -i, --ignore-case:匹配時忽略大小寫

       -v, --invert-match:反向匹配,只顯示不匹配的

       -e PATTERN, --regexp=PATTERN:使用多個正則表達式

       -o, --only-matching:只顯示匹配到的文本本身

       -q, --quiet, --silent:常用於腳本中

       -f FILE, --file=FILE:從文件中讀取正則表達式

       --color[=WHEN], --colour[=WHEN]:對匹配到文本着色後高亮顯示

       -E, --extended-regexp:使用擴展正則表達式匹配,相當於egrep

grep常用選項練習:

在/tmp/目錄下有個greptest.txt文件,其內容如下;

[root@localhost ~]# cat /tmp/greptest.txt 
This is first
how are you
How old are you 
fine,thanks
what,so what
What is you name
[root@localhost ~]#

1、匹配文件中包含字符“you”的行

[root@localhost ~]# grep "you" /tmp/greptest.txt 
how are you
How old are you 
What is you name
[root@localhost ~]#

2、匹配文件中包含字符“you”的行,且只輸出匹配的行。

[root@localhost ~]# grep -o "you" /tmp/greptest.txt 
you
you
you
[root@localhost ~]#

3、匹配文件中不包含字符“you”的行

[root@localhost ~]# grep -v "you" /tmp/greptest.txt 
This is first
fine,thanks
what,so what
[root@localhost ~]#

4、匹配文件中包含字符“what”的行且不區分大小寫

[root@localhost ~]# grep -i "what" /tmp/greptest.txt
what,so what
What is you name
[root@localhost ~]#

5、匹配文件中包含字符“fine”的行和下一行

[root@localhost ~]# grep -A 1   "fine" /tmp/greptest.txt
fine,thanks
what,so what
[root@localhost ~]#

6、匹配文件中包含字符“fine”的行和上一行

[root@localhost ~]# grep -B  1   "fine" /tmp/greptest.txt
How old are you 
fine,thanks
[root@localhost ~]#

7、匹配文件中包含字符“fine”的行和上下各一行

[root@localhost ~]# grep -C  1   "fine" /tmp/greptest.txt
How old are you 
fine,thanks
what,so what
[root@localhost ~]#

8、使用包含正則表達式的文件進行文本搜索

[root@localhost ~]# cat /tmp/grep
^f.*s$
[root@localhost ~]# grep -f /tmp/grep /tmp/greptest.txt 
fine,thanks
[root@localhost ~]#

9、多個正則表達式搜索文本

[root@localhost ~]# grep -e  "^H.*u" -e  "^f.*s$"  /tmp/greptest.txt 
How old are you 
fine,thanks
[root@localhost ~]#

grep經典練習:

在/tmp目錄下有一個文件greptest1.txt,其內容如下:

[root@localhost ~]# cat /tmp/greptest1.txt
He like his liker
He love his lover
138074082711
She love her lover
[email protected]
She like her lover
18618203761
12
234
19209783900
1.0.0.254
1.0.0.255
255
256
1.2.3.4
223.255.255.254
224.255.255.252
2.255.255.255
1329873909
[email protected]
12589098379
15608764083
[email protected]
jerry#[email protected]
[email protected]
13690876890

15820974619
[root@localhost ~]#

1、分組與引用練習,搜索同時包含love和lover的文本

[root@localhost ~]# grep -E ".+(l.v.).+\1r" /tmp/greptest1.txt
He love his lover
She love her lover
[root@localhost ~]#

2、數字查找練習,匹配1-255的數字

[root@localhost ~]# grep -E "^([1-9]|[1-9][1-9]|1[1-9][1-9]|2[1-4][1-9]|25[1-5])\$"  /tmp/greptest1.txt 
12
234
255
[root@localhost ~]#

3、ip地址查找練習,匹配ABC類IP地址即 1.0.0.1---223.255.255.254

[root@localhost ~]# grep -E "\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3])\.(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){2}([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\>" /tmp/greptest1.txt
1.0.0.254
1.2.3.4
223.255.255.254
[root@localhost ~]#

4、匹配Email地址:任意長度數字字母@任意長度數字字母.(com"org|net等等)

[root@localhost ~]# grep -E "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$" /tmp/greptest1.txt 
[email protected]
[email protected]
[email protected]
[email protected]
[root@localhost ~]#

5、匹配手機號碼:手機號碼是1[3|4|5|8]後面接9位數字的

[root@localhost ~]# grep -E  "\<1[3|4|5|8][0-9]{9}\>" /tmp/greptest1.txt 
18618203761
15608764083
13690876890
15820974619
[root@localhost ~]#


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章