Terminal-grep (搜索關(guān)鍵詞)

輸出當(dāng)前登錄用戶:

$ who
whp       console      Mar 15 14:23 
whp       ttys000      Mar 15 14:23 
whp       ttys001      Mar 15 14:23 

將當(dāng)前登錄用戶作為標(biāo)準(zhǔn)輸入, 通過grep 搜索關(guān)鍵詞‘000’:

$ who | grep 000
whp       ttys000      Mar 15 14:23 

-v 選項(xiàng): 匹配非‘000’行:

$ who | grep -v 000 
hepingwang       console      Mar 15 14:23 
hepingwang       ttys001      Mar 15 14:23 

指定文件phonebook搜索‘xu’ :

$ cat phonebook 
Aqqlice Chen    999-000
Charles wang    888-987
Jack xu     899-333

$ grep xu phonebook 
Jack xu     899-333

列出當(dāng)前目錄下所有文件:

$ ls
books       intro_sed   nu      passwd      sub_users   testNew     testNew3    users2
intro       names       numbers     phonebook   test1       testNew2    users

當(dāng)前路徑下所有文件, 搜索‘whp’ :

$ grep whp * 
sub_users:whp       ttys002      Mar 15 14:23 
sub_users:whp       ttys003      Mar 15 14:23 
users:whp       console      Mar 15 14:23 
users:whp       ttys000      Mar 15 14:23 
users:whp       ttys001      Mar 15 14:23 
users:whp       ttys002      Mar 15 14:23 
users:whp       ttys003      Mar 15 14:23 
users:whp       ttys004      Mar 15 14:23 
users2:whp       console      Mar 15 14:23 
users2:whp       ttys000      Mar 15 14:23 
users2:whp       ttys001      Mar 15 14:23 
users2:whp       ttys002      Mar 15 14:23 
users2:whp       ttys003      Mar 15 14:23 
users2:whp       ttys004      Mar 15 14:23 

-l 選項(xiàng), 列出包含 ‘whp’ 的文件

$ grep -l whp *  
sub_users
users
users2

-n 選項(xiàng),增加行號(hào)

$ grep -n whp users
1:whp       console      Mar 15 14:23 
2:whp       ttys000      Mar 15 14:23 
3:whp       ttys001      Mar 15 14:23 
4:whp       ttys002      Mar 15 14:23 
5:whp       ttys003      Mar 15 14:23 
6:whp       ttys004      Mar 15 14:23 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容