學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 操作系統(tǒng) > Linux教程 > 整理Linux中常用的一些grep命令介紹

整理Linux中常用的一些grep命令介紹

時(shí)間: 志藝942 分享

整理Linux中常用的一些grep命令介紹

  grep命令在對(duì)一個(gè)或多個(gè)文件的內(nèi)容進(jìn)行基于模式的搜索的情況下是非常有用的。模式可以是單個(gè)字符、多個(gè)字符、單個(gè)單詞、或者是一個(gè)句子。接下來(lái)是小編為大家收集的整理Linux中常用的一些grep命令介紹,希望能幫到大家。

  整理Linux中常用的一些grep命令介紹

  當(dāng)命令匹配到執(zhí)行命令時(shí)指定的模式時(shí),grep會(huì)將包含模式的一行輸出,但是并不對(duì)原文件內(nèi)容進(jìn)行修改。

  在本文中,我們將會(huì)討論到14個(gè)grep命令的例子。

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

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

  復(fù)制代碼

  代碼如下:

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

  linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash

  root@Linux-world:~#

  例2 在多個(gè)文件中查找模式。

  復(fù)制代碼

  代碼如下:

  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:$DdgXjxlMflz4JRvefvKp0DG6re: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:!::linuxtechi

  root@Linux-world:~#

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

  復(fù)制代碼

  代碼如下:

  root@Linux-world:~# grep -l linuxtechi /etc/passwd /etc/shadow /etc/fstab /etc/mtab

  /etc/passwd

  /etc/shadow

  root@Linux-world:~#

  例4 使用-n參數(shù),在文件中查找指定模式并顯示匹配行的行號(hào)

  復(fù)制代碼

  代碼如下:

  root@Linux-world:~# grep -n linuxtechi /etc/passwd

  39:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash

  root@Linux-world:~#

< p> root@Linux-world:~# grep -n root /etc/passwd /etc/shadow

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

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

  復(fù)制代碼

  代碼如下:

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

  例6 使用 ^ 符號(hào)輸出所有以某指定模式開(kāi)頭的行

  Bash腳本將 ^ 符號(hào)視作特殊字符,用于指定一行或者一個(gè)單詞的開(kāi)始。例如輸出/etc/passes文件中所有以“root”開(kāi)頭的行

  復(fù)制代碼

  代碼如下:

  root@Linux-world:~# grep ^root /etc/passwd

  root:x:0:0:root:/root:/bin/bash

  root@Linux-world:~#

  例7 使用 $ 符號(hào)輸出所有以指定模式結(jié)尾的行。

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

  復(fù)制代碼

  代碼如下:

  root@Linux-world:~# grep bash$ /etc/passwd

  root:x:0:0:root:/root:/bin/bash

  linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash

  root@Linux-world:~#

  Bash腳本將美元($)符號(hào)視作特殊字符,用于指定一行或者一個(gè)單詞的結(jié)尾。

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

  復(fù)制代碼

  代碼如下:

  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

  ............................................................................

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

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

  復(fù)制代碼

  代碼如下:

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

  root@Linux-world:~#

  由于/etc/shadow文件中沒(méi)有空行,所以沒(méi)有任何輸出

  例10 使用 -i 參數(shù)查找模式

  grep命令的-i參數(shù)在查找時(shí)忽略字符的大小寫(xiě)。

  我們來(lái)看一個(gè)例子,在paswd文件中查找“LinuxTechi”單詞。

  [code ]nextstep4it@localhost:~$ grep -i LinuxTechi /etc/passwd

  linuxtechi:x:1001:1001::/home/linuxtechi:/bin/bash

  nextstep4it@localhost:~$ [/code]

  例11 使用 -e 參數(shù)查找多個(gè)模式

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

  復(fù)制代碼

  代碼如下:

  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/bash

  root@Linux-world:~#

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

  首先,在當(dāng)前目錄中創(chuàng)建一個(gè)搜索模式文件“grep_pattern”,我想文件中輸入的如下內(nèi)容。

  復(fù)制代碼

  代碼如下:

  root@Linux-world:~# cat grep_pattern

  ^linuxtechi

  root

  false$

  root@Linux-world:~#

  現(xiàn)在,試試使用grep_pattern文件進(jìn)行搜索

  復(fù)制代碼

  代碼如下:

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

  例13 使用 -c 參數(shù)計(jì)算模式匹配到的數(shù)量

  繼續(xù)上面例子,我們?cè)趃rep命令中使用-c命令計(jì)算匹配指定模式的數(shù)量

  復(fù)制代碼

  代碼如下:

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

  22

  root@Linux-world:~#

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

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

  復(fù)制代碼

  代碼如下:

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

  b)使用-A參數(shù)輸出匹配行的后4行

  復(fù)制代碼

  代碼如下:

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

  c)使用-C參數(shù)輸出匹配行的前后各4行

  復(fù)制代碼

  代碼如下:

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


看了“整理Linux中常用的一些grep命令介紹”還想看:

1.Linux下如何使用grep命令搜索多個(gè)單詞

2.Linux中常用操作命令介紹

3.linux grep命令詳解

4.必學(xué)100個(gè)常用linux命令大全

2836757