使用awk筛选最后100行的日志

使用awk筛选最后100行的日志,awk,terminal,grep,command,Awk,Terminal,Grep,Command,我可以使用tail或grep过滤最后500行 tail --line 500 my_log | grep "ERROR" 使用awk的等效命令是什么 如何在下面的命令中添加行数 awk '/ERROR/' my_log 你能试试下面的吗 tac Input_file | awk 'FNR<=100 && /error/' | tac awk '/ERROR/{print FNR,$0}' Input_file 你能试试下面的吗 tac Input_file | aw

我可以使用tail或grep过滤最后500行

tail --line 500 my_log | grep "ERROR"
使用awk的等效命令是什么

如何在下面的命令中添加行数

awk '/ERROR/' my_log

你能试试下面的吗

tac Input_file | awk 'FNR<=100 && /error/' | tac
awk '/ERROR/{print FNR,$0}' Input_file

你能试试下面的吗

tac Input_file | awk 'FNR<=100 && /error/' | tac
awk '/ERROR/{print FNR,$0}' Input_file

由于您没有要测试的样本数据,我将使用
seq 1 10
仅显示数字。这个存储最后的
n
记录,并在最后打印出来:

$ seq 1 10 | 
  awk -v n=3 '{a[++c]=$0;delete a[c-n]}END{for(i=c-n+1;i<=c;i++)print a[i]}'
8
9
10

由于您没有要测试的样本数据,我将使用
seq 1 10
仅显示数字。这个存储最后的
n
记录,并在最后打印出来:

$ seq 1 10 | 
  awk -v n=3 '{a[++c]=$0;delete a[c-n]}END{for(i=c-n+1;i<=c;i++)print a[i]}'
8
9
10

直到读取文件的方式发生变化,awk才知道文件的结尾,但您可以读取文件的哪个部分,第一次找到结尾,第二次处理范围内的行。您也可以将X最后一行保留在缓冲区中,但它的内存消耗和进程有点大。请注意,该文件需要在结尾处提到两次

awk 'FNR==NR{L=NR-500;next};FNR>=L && /ERROR/{ print FNR":"$0}' my_log  my_log 
解释

awk '# first reading
     FNR==NR{
       #last line is this minus 500
       LL=NR-500
       # go to next line (for this file)
       next
       }

     # at second read (due to previous section filtering)
     # if line number is after(included) LL AND error is on the line content, print it
     FNR >= LL && /ERROR/ { print FNR ":" $0 }
     ' my_log  my_log 
论gnu-sed

sed '$-500,$ {/ERROR/ p}' my_log

直到读取文件的方式发生变化,awk才知道文件的结尾,但您可以读取文件的哪个部分,第一次找到结尾,第二次处理范围内的行。您也可以将X最后一行保留在缓冲区中,但它的内存消耗和进程有点大。请注意,该文件需要在结尾处提到两次

awk 'FNR==NR{L=NR-500;next};FNR>=L && /ERROR/{ print FNR":"$0}' my_log  my_log 
解释

awk '# first reading
     FNR==NR{
       #last line is this minus 500
       LL=NR-500
       # go to next line (for this file)
       next
       }

     # at second read (due to previous section filtering)
     # if line number is after(included) LL AND error is on the line content, print it
     FNR >= LL && /ERROR/ { print FNR ":" $0 }
     ' my_log  my_log 
论gnu-sed

sed '$-500,$ {/ERROR/ p}' my_log

可能重复的可能重复使用
a[NR%n]=$0
,然后使用
来(i=NR+1;ii如果最后100条经过处理的记录没有一行错误,那么如果我的会议困扰的想法正确,就不会有任何输出。这是正确的,但那将是类似于OP.right的。甚至不用费心重新阅读OP。你知道我们对ops的看法。:DIt可能更容易使用
a[NR%n]=$0
然后
for(i=NR+1;ii如果最后100条处理过的记录没有一条错误行,那么如果我的会议困扰着我,就不会有任何输出了。这是正确的,但这与OP类似。对。甚至不必费心重新阅读OP。你知道我们对OP的看法。:D