歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 14 個 grep 命令的例子

14 個 grep 命令的例子

日期:2017/2/28 14:01:33   编辑:Linux教程

概述:

所有的類Linux系統都會提供一個名為grep(global regular expression print,全局正則表達式輸出)的搜索工具。grep命令在對一個或多個文件的內容進行基於模式的搜索的情況下是非常有用的。模式可以是單個字符、多個字符、單個單詞、或者是一個句子。

當命令匹配到執行命令時指定的模式時,grep會將包含模式的一行輸出,但是並不對原文件內容進行修改。

在本文中,我們將會討論到14個grep命令的例子。

例1 在文件中查找模式(單詞)

在/etc/passwd文件中查找單詞“linuxtechi”

  1. root@Linux-world:~# grep linuxtechi /etc/passwd
  2. linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
  3. root@Linux-world:~#

例2 在多個文件中查找模式。

  1. root@Linux-world:~# grep linuxtechi /etc/passwd /etc/shadow /etc/gshadow
  2. /etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
  3. /etc/shadow:linuxtechi:$6$DdgXjxlM$4flz4JRvefvKp0DG6re:16550:0:99999:7:::/etc/gshadow:adm:*::syslog,linuxtechi
  4. /etc/gshadow:cdrom:*::linuxtechi
  5. /etc/gshadow:sudo:*::linuxtechi
  6. /etc/gshadow:dip:*::linuxtechi
  7. /etc/gshadow:plugdev:*::linuxtechi
  8. /etc/gshadow:lpadmin:!::linuxtechi
  9. /etc/gshadow:linuxtechi:!::
  10. /etc/gshadow:sambashare:!::linuxtechi
  11. root@Linux-world:~#

例3 使用-l參數列出包含指定模式的文件的文件名。

  1. root@Linux-world:~# grep -l linuxtechi /etc/passwd /etc/shadow /etc/fstab /etc/mtab
  2. /etc/passwd
  3. /etc/shadow
  4. root@Linux-world:~#

例4 使用-n參數,在文件中查找指定模式並顯示匹配行的行號

  1. root@Linux-world:~# grep -n linuxtechi /etc/passwd
  2. 39:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
  3. root@Linux-world:~#
  1. root@Linux-world:~# grep -n root /etc/passwd /etc/shadow

例5 使用-v參數輸出不包含指定模式的行

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

  1. root@Linux-world:~# grep -v linuxtechi /etc/passwd

例6 使用 ^ 符號輸出所有以某指定模式開頭的行

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

  1. root@Linux-world:~# grep ^root /etc/passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. root@Linux-world:~#

例7 使用 $ 符號輸出所有以指定模式結尾的行。

輸出/etc/passwd文件中所有以“bash”結尾的行。

  1. root@Linux-world:~# grep bash$ /etc/passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
  4. root@Linux-world:~#

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

例8 使用 -r 參數遞歸地查找特定模式

  1. root@Linux-world:~# grep -r linuxtechi /etc/
  2. /etc/subuid:linuxtechi:100000:65536
  3. /etc/group:adm:x:4:syslog,linuxtechi
  4. /etc/group:cdrom:x:24:linuxtechi
  5. /etc/group:sudo:x:27:linuxtechi
  6. /etc/group:dip:x:30:linuxtechi
  7. /etc/group:plugdev:x:46:linuxtechi
  8. /etc/group:lpadmin:x:115:linuxtechi
  9. /etc/group:linuxtechi:x:1000:
  10. /etc/group:sambashare:x:131:linuxtechi
  11. /etc/passwd-:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
  12. /etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
  13. ............................................................................

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

例9 使用 grep 查找文件中所有的空行

  1. root@Linux-world:~# grep ^$ /etc/shadow
  2. root@Linux-world:~#

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

例10 使用 -i 參數查找模式

grep命令的-i參數在查找時忽略字符的大小寫。

我們來看一個例子,在paswd文件中查找“LinuxTechi”單詞。

  1. nextstep4it@localhost:~$ grep -i LinuxTechi/etc/passwd
  2. linuxtechi:x:1001:1001::/home/linuxtechi:/bin/bash
  3. nextstep4it@localhost:~$

例11 使用 -e 參數查找多個模式

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

  1. root@Linux-world:~# grep -e "linuxtechi"-e "root"/etc/passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
  4. root@Linux-world:~#

例12 使用 -f 用文件指定待查找的模式

首先,在當前目錄中創建一個搜索模式文件“grep_pattern”,我想文件中輸入的如下內容。

  1. root@Linux-world:~# cat grep_pattern
  2. ^linuxtechi
  3. root
  4. false$
  5. root@Linux-world:~#

現在,試試使用grep_pattern文件進行搜索

  1. root@Linux-world:~# grep -f grep_pattern /etc/passwd

例13 使用 -c 參數計算模式匹配到的數量

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

  1. root@Linux-world:~# grep -c -f grep_pattern /etc/passwd
  2. 22
  3. root@Linux-world:~#

例14 輸出匹配指定模式行的前或者後面N行

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

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

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

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

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

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

grep使用簡明及正則表達式 http://www.linuxidc.com/Linux/2013-08/88534.htm

Linux下Shell編程——grep命令的基本運用 http://www.linuxidc.com/Linux/2013-06/85525.htm

grep 命令詳解及相關事例 http://www.linuxidc.com/Linux/2014-07/104041.htm

Linux基礎命令之grep詳解 http://www.linuxidc.com/Linux/2013-07/87919.htm

設置grep高亮顯示匹配項 http://www.linuxidc.com/Linux/2014-09/106871.htm

Linux grep命令學習與總結 http://www.linuxidc.com/Linux/2014-10/108112.htm

Copyright © Linux教程網 All Rights Reserved