AWK找到最后一场比赛并打印后N行 [英] awk find the last match and print the next N lines

查看:166
本文介绍了AWK找到最后一场比赛并打印后N行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下内容的日志文件:

 日期:2014年9月7日
价格:1.35
量:20
ProcessedBy:比尔其他一些内容日期:2014年9月8日
价格:10.1
量:15
ProcessedBy:爱丽丝其他一些内容日期:2014年9月9日
价格:100
金额:2.6
ProcessedBy:老板其他一些内容

我想用awk来找到最后的日期,并打印下面三行。

 日期:2014年9月9日
价格:100
金额:2.6
ProcessedBy:老板

我用code:

的awk'/日期/ X = NR}; END {NR> = X&放大器;&安培; NR< = X + 3} LOG_FILE

不过,似乎我不能把NR输出端之后。

我怎样才能获得最后一场比赛后,下面的N行?

感谢您的关注!


解决方案

  $的awk'/ ^日期:/ {C = 1; a = $ 0;}旁边℃下= 3 {C = C + 1; A = A\\ n$ 0} END {打印一个}'LOG_FILE
日期:2014年9月9日
价格:100
金额:2.6
ProcessedBy:老板

服用code一块在同一时间:


  • / ^日期:/ {C = 1; a = $ 0;接下来}

    每当一个开头的行日期:遇到,转让柜台 C 一,指定线路到变量 A ,并跳到下一行


  • C< = 3 {C = C + 1; A = A\\ n$ 0}

    如果计数器小于或等于三,增加计数器和新行保存到的变量结束 A


  • END {打印一个}

    打印的最后一个见过值 A


code这个问题的第二个版本

  $ AWK -v RS ='/ ^日期:/ {a = $ 0} END {打印一个}'LOG_FILE
日期:2014年9月9日
价格:100
金额:2.6
ProcessedBy:老板

服用code一块在同一时间:


  • -v RS =

    AWK 把它的输入成的记载。该作品被记录分隔符 RS 设置为一个空行。 (对于GNU AWK ,一个空 RS 匹配空行。对于的awk的其他版本,你可能需要做不同的事情。)


  • / ^日期:/ {a = $ 0}

    遇到一个记录每次与开始日期:,它被保存在变量 A


  • END {打印一个}

    在运行结束,最后见过的值 A 打印。


code这个问题的第一个版本

  $ AWK -v RS ='END {$打印0}LOG_FILE
日期:2014年9月9日
价格:100
金额:2.6
ProcessedBy:老板

I have a log file which contains the following content:

Date: 2014-09-07
Price: 1.35
Amount: 20
ProcessedBy: Bill

Some other contents

Date: 2014-09-08
Price: 10.1
Amount: 15
ProcessedBy: Alice

Some other contents

Date: 2014-09-09
Price: 100
Amount: 2.6
ProcessedBy: Boss

Some other contents

I would like to use awk to find the last "Date", and print the following three lines.

Date: 2014-09-09
Price: 100
Amount: 2.6
ProcessedBy: Boss

I use the code:

awk '/Date/ {x=NR}; END{NR>=x && NR<=x+3} LOG_FILE

However, it seems that I cannot put NR output after END.

How can I get the following N lines after the last match?

Thank you for your attention!

解决方案

$ awk  '/^Date:/ {c=1; a=$0;next} c<=3{c=c+1;a=a"\n"$0}END{print a}' LOG_FILE
Date: 2014-09-09
Price: 100
Amount: 2.6
ProcessedBy: Boss

Taking the code one piece at a time:

  • /^Date:/ {c=1; a=$0;next}

    Every time that a line starting with Date: is encountered, assign counter c to one, assign the line to the variable a, and skip to the next line

  • c<=3{c=c+1;a=a"\n"$0}

    If the counter is less than or equal to three, increment the counter and save the new line to the end of variable a.

  • END{print a}

    Print the last-seen value of a.

Code for the second version of this question

$ awk -v RS=  '/^Date:/ {a=$0} END{print a}' LOG_FILE
Date: 2014-09-09
Price: 100
Amount: 2.6
ProcessedBy: Boss

Taking the code one piece at a time:

  • -v RS=

    awk divides its input up into "records." This works by setting the record separator RS to a blank line. (For GNU awk, an empty RS matches an empty line. For other versions of awk, you may need to do something different.)

  • /^Date:/ {a=$0}

    Every time a record is encountered that starts with Date:, it is saved in the variable a.

  • END{print a}

    At the end of the run, the last-seen value of a is printed.

Code for first version of this question

$ awk -v RS=  'END{print $0}' LOG_FILE
Date: 2014-09-09
Price: 100
Amount: 2.6
ProcessedBy: Boss

这篇关于AWK找到最后一场比赛并打印后N行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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