⑴[rootlocalhost test]# cat test.txt
⑵peida.blogs.
⑶ubuntu linux
⑷linuxmint
⑸[rootlocalhost test]# cat test.txt
⑹[rootlocalhost test]# cat test.txt | grep -nf test.txt
⑺:hnlinux
⑻:ubuntu linux
⑼:linuxmint
⑽[rootlocalhost test]#
⑾输出test.txt文件中含有从test.txt文件中读取出的关键词的内容行,并显示每一行的行号
⑿实例:从文件中查找关键词
⒀命令:grep ‘linux’ test.txt
⒁[rootlocalhost test]# grep ‘linux’ test.txt
⒂ubuntu linux
⒃linuxmint
⒄[rootlocalhost test]# grep -n ‘linux’ test.txt
⒅:hnlinux
⒆:ubuntu linux
⒇:linuxmint
⒈[rootlocalhost test]#
⒉实例:从多个文件中查找关键词
⒊命令:grep ‘linux’ test.txt test.txt
⒋[rootlocalhost test]# grep -n ‘linux’ test.txt test.txt
⒌test.txt::hnlinux
⒍test.txt::ubuntu linux
⒎test.txt::linuxmint
⒏test.txt::linux
⒐[rootlocalhost test]# grep ‘linux’ test.txt test.txt
⒑test.txt:hnlinux
⒒test.txt:ubuntu linux
⒓test.txt:linuxmint
⒔test.txt:linux
⒕[rootlocalhost test]#
⒖说明:多文件时,输出查询到的信息内容行时,会把文件的命名在行最前面输出并且加上“:”作为标示符
⒗实例:grep不显示本身进程
⒘ps aux|grep [s]sh
⒙ps aux | grep ssh | grep -v “grep”
⒚[rootlocalhost test]# ps aux|grep ssh
⒛root . . ? Ss Nov : /usr/sbin/sshd
①root . . ? Ss : : sshd: 《a href=“mailto:rootpts/”》rootpts/《/a》
②root . . pts/ S+ : : grep ssh
③[rootlocalhost test]# ps aux|grep [s]sh]
④[rootlocalhost test]# ps aux|grep [s]sh
⑤root . . ? Ss Nov : /usr/sbin/sshd
⑥root . . ? Ss : : sshd: 《a href=“mailto:rootpts/”》rootpts/《/a》
⑦[rootlocalhost test]# ps aux | grep ssh | grep -v “grep”
⑧root . . ? Ss Nov : /usr/sbin/sshd
⑨root . . ? Ss : : sshd: 《a href=“mailto:rootpts/”》rootpts/《/a》