# 查看实时日志并过滤
tail -f test.log |grep -a '关键字'
# 过滤关键字数据,追加到文件
cat -n test.log | grep "关键字" > error.log
因为通常时候我们用grep拿到的日志很少,我们需要查看附近的日志.
我是这样做的,首先得到关键日志的行号
cat -n test.log | grep "地形"
如得到"地形"关键字所在的行号是102行. 此时如果我想查看这个关键字前10行和后10行的日志:
cat -n test.log |tail -n +92|head -n 20
tail -n +92表示查询92行之后的日志
head -n 20则表示在前面的查询结果里再查前20条记录
sed -n '/2017-02-23 10:00:00/,/2017-02-23 10:06:23/p' test.log
sed -n '/2017-02-23 10:00:/,/2017-02-23 10:06:/p' test.log
这个需要注意的地方是10:00:00或者10:00必须出现过.
如果没有任何日志出现.请先查看是否存在
grep "2017-02-23 10:00" test.log
grep "abcd" test.log | wc -l

tail -f test.log
tail -f test.log |grep -a '关键字'
tail -n 20 test.log | grep '结果'
# 查询最后20行,并查找关键字“结果”,文字标红
tail -n 20 test.log | grep '结果' --color
# 查询最后20行,并查找关键字“结果”,文字标红,上下扩展两行
tail -n 20 test.log | grep '结果' --color -a2
cat -n test.log | grep "error" | more
vim test.log 打开文件
ctrl + g 移动至文件末尾
:? com.fm.fdata 从最后往上查找关键字,点N往上