99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

14個grep命令的例子。

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

博羅網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司2013年開創(chuàng)至今到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司

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

在/etc/passwd文件中查找單詞“l(fā)inuxtechi”

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參數(shù)列出包含指定模式的文件的文件名。

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

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

root@Linux-world:~# grep -n linuxtechi /etc/passwd39:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bashroot@Linux-world:~#
root@Linux-world:~# grep -n root /etc/passwd /etc/shadow

14個grep命令的例子。

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

輸出/etc/passwd文件中所有不含單詞“l(fā)inuxtechi”的行

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

14個grep命令的例子。

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

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

root@Linux-world:~# grep ^root /etc/passwdroot:x:0:0:root:/root:/bin/bashroot@Linux-world:~#

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

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

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

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

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

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”單詞

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

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

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

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

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

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

nextstep4it@localhost:~$ grep -i LinuxTechi /etc/passwdlinuxtechi:x:1001:1001::/home/linuxtechi:/bin/bashnextstep4it@localhost:~$

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

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

root@Linux-world:~# grep -e "linuxtechi" -e "root" /etc/passwdroot:x:0:0:root:/root:/bin/bashlinuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bashroot@Linux-world:~#

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

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

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

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

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

14個grep命令的例子。

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

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

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

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

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

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

14個grep命令的例子。

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

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

14個grep命令的例子。

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

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

14個grep命令的例子。

文章名稱:14個grep命令的例子。
分享鏈接:http://www.yijiale78.com/article28/joopjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司網(wǎng)站排名自適應(yīng)網(wǎng)站微信公眾號域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)