Unix的帮助,以提取/打印每3模式出现,直到文件结束后50行 [英] Unix help to extract/print 50 lines after every 3rd occurrence pattern till end of file

查看:140
本文介绍了Unix的帮助,以提取/打印每3模式出现,直到文件结束后50行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提取/打印每3模式出现,直到文件结束后4行帮助。考虑下面的例子是日志文件的

  ERROR_FILE_NOT_FOUND
ERROR_FILE_NOT_FOUND
ERROR_FILE_NOT_FOUND
提取行1
提取2号线
提取3号线
提取4号线ERROR_FILE_NOT_FOUND
ERROR_FILE_NOT_FOUND
ERROR_FILE_NOT_FOUND
提取5号线
提取6号线
提取7号线
提取8号线ERROR_FILE_NOT_FOUND
ERROR_FILE_NOT_FOUND
ERROR_FILE_NOT_FOUND
提取9号线
提取10行
提取11行
提取12行


解决方案

只是用一些标志来跟踪哪些出现多少次:

 的awk -v PATT =ERROR_FILE_NOT_FOUND
    行== 3 {打印;出现++}
     出现== 4 {行= 0;出现= 0}
     PATT〜$ {0线++}'文件

说明

这会持续作为匹配的行数。一旦达到 3 ,开始打印。它打印准确 4 倍。


  • -v PATT =ERROR_FILE_NOT_FOUND这提供了模式来看待

  • 行== 3 {打印;出现++} 如果计数器 3 ,打印线,并开始计算多少线路纷纷亮相。

  • 出现== 4 {行= 0;出现= 0} 如果已打印行数已经是 4 ,标志重置为 0

  • PATT〜$ {0线++} 如果行给定的模式,增加变量

请注意所有这些常量 3 4 可与 -v <外放/ code>为好,以使其更通用的:

 的awk -v PATT =ERROR_FILE_NOT_FOUND-v匹配= 3 -v lines_to_print = 4
    行== {匹配打印;出现++}
     出现== lines_to_print {行= 0;出现= 0}
     PATT〜$ {0线++}'文件

测试

  $ AWK -v PATT =ERROR_FILE_NOT_FOUND'行== 3 {打印;出现++} ==出现4 {行= 0;出现= 0} PATT〜$ {0线++}'文件
提取行1
提取2号线
提取3号线
提取4号线
提取5号线
提取6号线
提取7号线
提取8号线
提取9号线
提取10行
提取11行
提取12行

I need help with extract/print 4 lines after every 3rd occurrence pattern till end of file. Consider below is example of log file

ERROR_FILE_NOT_FOUND
ERROR_FILE_NOT_FOUND
ERROR_FILE_NOT_FOUND
Extract line 1
Extract line 2
Extract line 3
Extract line 4

ERROR_FILE_NOT_FOUND
ERROR_FILE_NOT_FOUND
ERROR_FILE_NOT_FOUND
Extract line 5
Extract line 6
Extract line 7
Extract line 8

ERROR_FILE_NOT_FOUND
ERROR_FILE_NOT_FOUND
ERROR_FILE_NOT_FOUND
Extract line 9
Extract line 10
Extract line 11
Extract line 12

解决方案

Just use some flags to keep track of what appeared and how many times:

awk -v patt="ERROR_FILE_NOT_FOUND"
    'lines==3 {print; appeared++}
     appeared==4 {lines=0;appeared=0}
     patt~$0 {lines++}' file

Explanation

This keeps loading lines as number of lines matched. Once it has reached 3, starts printing. It prints exactly 4 times.

  • -v patt="ERROR_FILE_NOT_FOUND" this provides the pattern to be looked at
  • lines==3 {print; appeared++} if the counter lines is 3, print the line and start counting how many lines have appeared.
  • appeared==4{lines=0;appeared=0} if the number of lines that have been printed is already 4, reset the flags to 0.
  • patt~$0 {lines++} if the line matches the pattern given, increment the variable lines.

Note all these constants 3 and 4 could be put outside with -v as well, to make it more generic:

awk -v patt="ERROR_FILE_NOT_FOUND" -v matches=3 -v lines_to_print=4
    'lines==matches {print; appeared++}
     appeared==lines_to_print {lines=0;appeared=0}
     patt~$0 {lines++}' file

Test

$ awk -v patt="ERROR_FILE_NOT_FOUND" 'lines==3 {print; appeared++} appeared==4 {lines=0;appeared=0} patt~$0 {lines++}' file
Extract line 1
Extract line 2
Extract line 3
Extract line 4
Extract line 5
Extract line 6
Extract line 7
Extract line 8
Extract line 9
Extract line 10
Extract line 11
Extract line 12

这篇关于Unix的帮助,以提取/打印每3模式出现,直到文件结束后50行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆