输出当前登录用户:
$ who
whp console Mar 15 14:23
whp ttys000 Mar 15 14:23
whp ttys001 Mar 15 14:23
将当前登录用户作为标准输入, 通过grep 搜索关键词‘000’:
$ who | grep 000
whp ttys000 Mar 15 14:23
-v 选项: 匹配非‘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
列出当前目录下所有文件:
$ ls
books intro_sed nu passwd sub_users testNew testNew3 users2
intro names numbers phonebook test1 testNew2 users
当前路径下所有文件, 搜索‘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 选项, 列出包含 ‘whp’ 的文件
$ grep -l whp *
sub_users
users
users2
-n 选项,增加行号
$ 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