如何快速地在滚动的web日志中查询自己想要的信息

一.文件搜索

1.1 搜索指定字符串

grep 'exception' info.log

在文件info.logo中搜索字符串’exception’。

1.2 搜索字符串并显示其前后几行的内容

grep -A 30 'text'  info.log  #显示后30行
grep -B 30 'text'  info.log  #显示前30行
grep -C 30 'text'  info.log  #显示前后30行

1.3 搜索字符串只返回第一个匹配结果

grep -m 1 'text' info.log  #返回第一个结果
grep -m 2 'text' info.log  #返回前两个结果,以此类推
grep -m 1 -A 30 'text' info.log #返回第一个结果,并显示其后39行内容

1.4 搜索匹配str1或str2的行

使用|隔开不同的搜索词,注意要用\进行转义
如:

grep “str1\|str2” info.log

二.文件查看

2.1 实时查看日志输出

tail - f info.log

2.2 实时查看日志正在输出的错误

 #假设错误信息中包含ERROR
tail -f info.log | grep 'ERROR' 

三.其它命令

3.1 统计行数

wc -l info.log

猜你喜欢

转载自blog.csdn.net/vxzhg/article/details/100013393