grep基本用法
简介:grep命令用于在指定文件中查找匹配到的行,常与其他命令组合管道符使用
用法:
grep 参数 匹配关键字 指定文件
常用的参数:
-v 显示与关键字不匹配的行
-c 统计匹配到的行数
-n 显示行号
-r 递归搜索
-i 忽略大小写
例子:
1、查找显示error.log文件中的ERROR
grep "ERROR" error.log
2、查看java进程
ps -ef | grep java
3、查看nohup.out日志中匹配hello关键字的前10行和后20行
grep -n "hello" -A20 -B10 nohup.out
4、查看nohup.out文件中以hello开头的行
grep ^hello nohup.out
注:egrep支持正则匹配(grep -E)
例子:
查看nohup.out文件中包含hello或者world的行
1、egrep -n "hello|world" nohup.out
2、grep -En "hello|world" nohup.out

浙公网安备 33010602011771号