掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流
grep 指令用于查找內(nèi)容包含指定的范本樣式的文件,如果發(fā)現(xiàn)某文件的內(nèi)容符合所指定的范本樣式,預設 grep 指令會把含有范本樣式的那一列顯示出來,下面為大家分享一下grep命令使用實例。

成都創(chuàng)新互聯(lián)成都網(wǎng)站建設按需定制制作,是成都網(wǎng)站維護公司,為格柵板提供網(wǎng)站建設服務,有成熟的網(wǎng)站定制合作流程,提供網(wǎng)站定制設計服務:原型圖制作、網(wǎng)站創(chuàng)意設計、前端HTML5制作、后臺程序開發(fā)等。成都網(wǎng)站建設熱線:18982081108
在/etc/passwd文件中查找單詞“l(fā)inuxtechi”
root@Linux-world:~# grep linuxtechi /etc/passwd
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
root@Linux-world:~#
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:!::linuxtechi
root@Linux-world:~#
root@Linux-world:~# grep -l linuxtechi /etc/passwd /etc/shadow /etc/fstab /etc/mtab
/etc/passwd
/etc/shadow
root@Linux-world:~#
root@Linux-world:~# grep -n linuxtechi /etc/passwd
39:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
root@Linux-world:~#
輸出/etc/passwd文件中所有不含單詞“l(fā)inuxtechi”的行
root@Linux-world:~# grep -v linuxtechi /etc/passwd
Bash腳本將 ^ 符號視作特殊字符,用于指定一行或者一個單詞的開始。例如輸出/etc/passes文件中所有以“root”開頭的行
root@Linux-world:~# grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash
root@Linux-world:~#
輸出/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/bash
root@Linux-world:~#
Bash腳本將美元($)符號視作特殊字符,用于指定一行或者一個單詞的結尾
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目錄中查找“l(fā)inuxtechi”單詞
root@Linux-world:~# grep ^$ /etc/shadow
root@Linux-world:~#
由于/etc/shadow文件中沒有空行,所以沒有任何輸出
grep命令的-i參數(shù)在查找時忽略字符的大小寫。我們來看一個例子,在paswd文件中查找“LinuxTechi”單詞
nextstep4it@localhost:~$ grep -i LinuxTechi /etc/passwd
linuxtechi:x:1001:1001::/home/linuxtechi:/bin/bash
nextstep4it@localhost:~$
例如,我想在一條grep命令中查找‘linuxtechi’和‘root’單詞,使用-e參數(shù),我們可以查找多個模式
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:~#
首先,在當前目錄中創(chuàng)建一個搜索模式文件“grep_pattern”,我想文件中輸入的如下內(nèi)容
root@Linux-world:~# cat grep_pattern
^linuxtechi
root
false$
root@Linux-world:~#
現(xiàn)在,試試使用grep_pattern文件進行搜索
root@Linux-world:~# grep -f grep_pattern /etc/passwd
繼續(xù)上面例子,我們在grep命令中使用-c命令計算匹配指定模式的數(shù)量
root@Linux-world:~# grep -c -f grep_pattern /etc/passwd
22
root@Linux-world:~#
a)使用-B參數(shù)輸出匹配行的前4行
root@Linux-world:~# grep -B 4"games" /etc/passwd
b)使用-A參數(shù)輸出匹配行的后4行
root@Linux-world:~# grep -A 4"games" /etc/passwd
c)使用-C參數(shù)輸出匹配行的前后各4行
root@Linux-world:~# grep -C 4"games" /etc/passwd

我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流