grep命令简介

grep 命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并
把匹配的行打印出来。
格式:grep [option] pattern [file]
可使用 —help 查看更多参数。
使用实例:
ps -ef | grep sshd 查找指定 ssh 服务进程
ps -ef | grep sshd | grep -v grep 查找指定服务进程,排除 gerp 本身
ps -ef | grep sshd –c 查找指定进程个数
cat a.txt | grep -f b.txt 从文件中读取关键词进行搜索
输出 a.txt 文件中含有从 b.txt 文件中读取出的关键词的内容行
cat a.txt | grep –nf b.txt 从文件中读取关键词进行搜索,显示行号
grep -n 'linux' test.txt 从文件中查找关键词,并显示行号
cat test.txt |grep ^u 找出以 u 开头的行内容
cat test.txt |grep ^[^u] 输出非 u 开头的行内容
cat test.txt |grep hat$ 输出以 hat 结尾的行内容
cat test.txt |grep -E "ed|at" 显示包含 ed 或者 at 字符的内容行

猜你喜欢

转载自java-doom.iteye.com/blog/2403785