巧用Grep 命令

來源:互聯網
上載者:User

巧用Grep 命令
導讀所有的類linux系統都會提供一個名為grep(global regular expression print,全域Regex輸出)的搜尋工具。grep命令在對一個或多個檔案的內容進行基於模式的搜尋的情況下是非常有用的。模式可以是單個字元、多個字元、單個單詞、或者是一個句子。當命令匹配到執行命令時指定的模式時,grep會將包含模式的一行輸出,但是並不對原檔案內容進行修改。例1 在檔案中尋找模式(單詞)

在/etc/passwd檔案中尋找單詞“linuxtechi”

root@Linux-world:~# grep linuxtechi /etc/passwdlinuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bashroot@Linux-world:~#
例2 在多個檔案中尋找模式
root@Linux-world:~# grep linuxtechi /etc/passwd /etc/shadow /etc/gshadow    /etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash    /etc/shadow:linuxtechi:$6$DdgXjxlM$4flz4JRvefvKp0DG6re:16550:0:99999:7:::/etc/gshadow:adm:*::syslog,linuxtechi    /etc/gshadow:cdrom:*::linuxtechi    /etc/gshadow:sudo:*::linuxtechi    /etc/gshadow:dip:*::linuxtechi    /etc/gshadow:plugdev:*::linuxtechi    /etc/gshadow:lpadmin:!::linuxtechi    /etc/gshadow:linuxtechi:!::    /etc/gshadow:sambashare:!::linuxtechiroot@Linux-world:~#
例3 使用-L參數列出包含指定模式的檔案的檔案名稱
root@Linux-world:~# grep -l linuxtechi /etc/passwd /etc/shadow /etc/fstab /etc/mtab    /etc/passwd    /etc/shadowroot@Linux-world:~#
例4 使用-N參數,在檔案中尋找指定模式並顯示匹配行的行號
root@Linux-world:~# grep -n linuxtechi /etc/passwd    39:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bashroot@Linux-world:~#
例5 使用-V參數輸出不包含指定模式的行

輸出/etc/passwd檔案中所有不含單詞“linuxtechi”的行

root@Linux-world:~# grep -v linuxtechi /etc/passwd
例6 使用 ^ 符號輸出所有以某指定模式開頭的行

Bash指令碼將 ^ 符號視作特殊字元,用於指定一行或者一個單詞的開始。例如輸出/etc/passes檔案中所有以“root”開頭的行

root@Linux-world:~# grep ^root /etc/passwd    root:x:0:0:root:/root:/bin/bashroot@Linux-world:~#
例7 使用 $ 符號輸出所有以指定模式結尾的行

輸出/etc/passwd檔案中所有以“bash”結尾的行

root@Linux-world:~# grepbash$ /etc/passwd    root:x:0:0:root:/root:/bin/bash    linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bashroot@Linux-world:~#

Bash指令碼將美元($)符號視作特殊字元,用於指定一行或者一個單詞的結尾

例8 使用 -R 參數遞迴地尋找特定模式
root@Linux-world:~# grep -r linuxtechi /etc/    /etc/subuid:linuxtechi:100000:65536    /etc/group:adm:x:4:syslog,linuxtechi    /etc/group:cdrom:x:24:linuxtechi    /etc/group:sudo:x:27:linuxtechi    /etc/group:dip:x:30:linuxtechi    /etc/group:plugdev:x:46:linuxtechi    /etc/group:lpadmin:x:115:linuxtechi    /etc/group:linuxtechi:x:1000:    /etc/group:sambashare:x:131:linuxtechi    /etc/passwd-:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash    /etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash............................................................................

上面的命令將會遞迴的在/etc目錄中尋找“linuxtechi”單詞

例9 使用 Grep 尋找檔案中所有的空行
root@Linux-world:~# grep ^$ /etc/shadowroot@Linux-world:~#

由於/etc/shadow檔案中沒有空行,所以沒有任何輸出

例10 使用 -I 參數尋找模式

grep命令的-i參數在尋找時忽略字元的大小寫。我們來看一個例子,在paswd檔案中尋找“LinuxTechi”單詞

nextstep4it@localhost:~$ grep -i LinuxTechi /etc/passwd    linuxtechi:x:1001:1001::/home/linuxtechi:/bin/bashnextstep4it@localhost:~$
例11 使用 -E 參數尋找多個模式

例如,我想在一條grep命令中尋找‘linuxtechi’和‘root’單詞,使用-e參數,我們可以尋找多個模式

root@Linux-world:~# grep -e "linuxtechi" -e "root" /etc/passwd    root:x:0:0:root:/root:/bin/bash    linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bashroot@Linux-world:~#
例12 使用 -F 用檔案指定待尋找的模式

首先,在目前的目錄中建立一個搜尋模式檔案“grep_pattern”,我想檔案中輸入的如下內容

root@Linux-world:~# cat grep_pattern    ^linuxtechi    root    false$root@Linux-world:~#

現在,試試使用grep_pattern檔案進行搜尋

root@Linux-world:~# grep -f grep_pattern /etc/passwd
例13 使用 -C 參數計算模式比對到的數量

繼續上面例子,我們在grep命令中使用-c命令計算匹配指定模式的數量

root@Linux-world:~# grep -c -f grep_pattern /etc/passwd    22root@Linux-world:~#
例14 輸出匹配指定模式行的前或者後面N行

a)使用-B參數輸出匹配行的前4行

root@Linux-world:~# grep -B 4"games" /etc/passwd

b)使用-A參數輸出匹配行的後4行

root@Linux-world:~# grep -A 4"games" /etc/passwd

c)使用-C參數輸出匹配行的前後各4行

root@Linux-world:~# grep -C 4"games" /etc/passwd

原文來自:http://www.tiejiang.org/14654.html

本文地址:http://www.linuxprobe.com/grep-command.html


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.