linux及mac查看某关键词前后几行内容

MAC log文件 汇总关键字行的前一行所有数据

> 直接把内容生成到指定文件,会覆盖源文件中的内容,还有一种用途是直接生成一个空白文件,相当于touch命令

>>尾部追加,不会覆盖掉文件中原有的内容

1、cat /Users/cdtanghui3/Downloads/248329.log |grep "ERROR GroovyPerfTestEngineWorker -" -B1>/Users/cdtanghui3/Downloads/test.txt

2、cat /Users/cdtanghui3/Downloads/test.txt|grep "ERROR TestUtils - http" -B0>/Users/cdtanghui3/Downloads/test1.txt

3、awk 'BEGIN{FS=" "}''{print $7}' /Users/cdtanghui3/Downloads/test1.txt >/Users/cdtanghui3/Downloads/test2.txt

 

汇总:

1、一个比较方便直接的办法

 

查看filename中含有abc所在行后4行内容

 $cat filename | grep abc -A4

查看filename中含有abc所在行前4行内容

 $cat filename  | grep abc  -B4

 

2、先查到你要查的那个词所在行,然后根据行号查看临近几行内容

 

查看filename中abc所在行前两行内容

$cat -n filename | grep abc

这一步可以得到abc所在行,然后根据这个行号人工将行号分别减1减2来查看行号

 

查看某文件中指定第几行内容可以用

原文地址:https://www.cnblogs.com/Tanwheey/p/13084072.html