首頁 php教程 PHP开发 Grep指令詳解-9個經典使用場景

Grep指令詳解-9個經典使用場景

Dec 13, 2016 pm 02:54 PM

Grep
全名為Global Regular Expression Print,表示全域正規表示式
是強大的文字搜尋工具,採用正規比對
1、指令格式
grep [options] files


2、主要參數
-c匹配行的數目
-i: 不區分大小寫
-n:顯示匹配航以及行號
-l:查詢多文件的時候只輸出包含匹配字符的文件名
-v:反向匹配,即顯示不符合的行
-h: 查詢的時候不適用檔案名稱
-s:不顯示錯誤訊息


3、部分正規表示式
    反義字元:如""""表示符合""
^$ 開始和結束
[] 單個字符,[A]
[ - ] 匹配一個範圍,[0-9a-zA-Z]匹配所有數字和字母
* 前面的字符出現0次或者多次
+ 前面的字符出現了一次或者多次
. 任意字符

4、經典場景

除非要精確區分大小寫,否則請加上-i來忽略大小寫

(1)結合find命令和管道
  你的一個音樂資料夾裡有多種格式的文件,而你只想找到藝術家jay的mp3文件,並且不含有任何的混合音軌
[root@localhost ~]#find . -name ".mp3" | grep -i jay | grep -vi "remix"
分析: 1)使用find -name 列出所有mp3文件,重定向給grep
2) 使用grep -i 來查找包含jay的行
3)使用grep -vi 來查找不包含remix的行


(2)-A -B -C
  很多時候,我們並關心匹配行而是關心匹配行的上下文。這時候-A -B -C就有用了
-A n 後n行,A記憶為(After)
-B n 前n行,B記憶為(Before)
-C n 前n行,後n行,C記憶為(Center)
舉例

[root@localhost ~]# ifconfig | grep -A 2 "Link encap"
eth0      Link encap:Ethernet  HWaddr 00:0C:29:F3:38:15  
          inet addr:192.168.91.129  Bcast:192.168.91.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fef3:3815/64 Scope:Link
--
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host


[root@localhost ~]#  ifconfig | grep -C 2 "lo"
          Interrupt:67 Base address:0x2024 


lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
登入後複製

(3) 用-c來統計數目
  你手邊有一個很大的文件,這個文件包含網址,例如www.baidu.com tieba.baidu.com等等。你想要知道有多少個隸屬於百度的網址
[

root@localhost ~]# grep -c "*baidu.com*" filename
例子
[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -cn ".*baidu.com.*" file.txt 
3
登入後複製

(4) -r 遞歸查找子目錄
  查找當前目錄極度子目錄下麵包含匹配字符的文件
查找子目錄,匹配後輸出行號,這裡的點表示目前目錄

[root@localhost ~]# grep -nr HELLO_HWC_CSND_BLOG* .

範例:

[root@localhost ~]# grep -nr baidu .
./file.txt:8:www.baidu.com
./file.txt:9:tieba.baidu.com
./file.txt:11:www.baidu.com/search/index
./test/test.txt:1:http://www.baidu.com
登入後複製

查找子目錄,符合後只輸出檔案名稱
[

[root@localhost ~]# grep -lr baidu .
./file.txt
./test/test.txt
登入後複製

查找子目錄,符合後只輸出檔案名稱

[ groot@locallr HELLO_HWC_CSND_BLOG* .


例子:

[root@localhost ~]# ps aux | grep init
root         1  0.0  0.1   2072   632 ?        Ss   22:52   0:01 init [5]                             
root      4210  0.0  0.1   6508   620 ?        Ss   23:01   0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients"
root      4233  0.0  0.0   2780   504 ?        S    23:01   0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients
root      4956  0.0  0.1   3920   680 pts/1    R+   23:27   0:00 grep init
登入後複製

(5)--line-buffered 打開buffering 模式
  你有一個文件是動態的,它不斷地添加信息到文件的尾部,而你想要輸出包含某些信息的行。即持續的grep一個動態的流


[root@localhost ~]#tail -f file | grep --line-buffered your_pattern 


(6)結合ps ine

[root@localhost ~]# ps aux | grep [i]nit
root         1  0.0  0.1   2072   632 ?        Ss   22:52   0:01 init [5]                             
root      4210  0.0  0.1   6508   620 ?        Ss   23:01   0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients"
root      4233  0.0  0.0   2780   504 ?        S    23:01   0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients
登入後複製



(6)結合ps ine

我們看到了gre執行的指令也被列出來了

如果不想要這一行,我們可以這麼改指令

[root@localhost ~]# ls
anaconda-ks.cfg  Desktop  file.txt  find.result  install.log  install.log.syslog  test
[root@localhost ~]# grep -r baidu .
./file.txt:www.baidu.com
./file.txt:tieba.baidu.com
./file.txt:www.baidu.com/search/index
./test/test.txt:http://www.baidu.com
登入後複製

(7)找出不包含某一個目錄

[root@localhost ~]#grep -R --exclude-dir=node_modules 'some pattern' /path/to/search

例子

[root@localhost ~]# grep -R --exclude-dir=text "baidu" .
./file.txt:www.baidu.com
./file.txt:tieba.baidu.com
./file.txt:www.baidu.com/search/index
登入後複製

這時候如果我們不想包含test目錄

grep: unrecognized option `--exclude-dir=test'
登入後複製

如果報錯

[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}" file.txt
192.168.0.1
162.12.0.123
登入後複製

說明版本過老,更新下就ok
如果報錯
查找IP位址
這裡用到了-o和-P指令
我們透過man grep查看
-o, --only-matching:
              Show only the part of a matching line that matches PATN. -- regexp:
              Interpret PATTERN as a Perl regular expression.

也就是說-o,只顯示配對行中符合正規表示式的那部分

-P,作為Perl正則符合查找@localhost ~]# grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+" file. txt


範例

[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+" file.txt
njuhwc@163.com
njuhwc@gmil.com
登入後複製

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24