輸出當(dāng)前登錄用戶(hù):
$ who
whp console Mar 15 14:23
whp ttys000 Mar 15 14:23
whp ttys001 Mar 15 14:23
將當(dāng)前登錄用戶(hù)作為標(biāo)準(zhǔn)輸入, 通過(guò)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