awk find the last match and print the next N lines

Starry

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!

John1024
$ 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

awk find the last match and print the next N lines

From Dev

awk match last record and print

From Dev

sed script to print n lines after the last occurence of a match

From Dev

awk: print lines after match to end of file

From Dev

awk exact variable match, print remaining lines

From Dev

awk print 2 lines back if match

From Dev

Print the first and last match of a field with awk

From Dev

Awk command to print all the lines except the last three lines

From Dev

Awk command to print all the lines except the last three lines

From Dev

ed print the next n lines as succinctly as possible

From Dev

awk - delete line if next lines don't match pattern

From Dev

Print lines from match until end of file with awk

From Dev

awk compare 2 files, print match and nonmatch lines

From Dev

awk + print lines from the first line until match word

From Dev

awk compare 2 files, print match and nonmatch lines

From Dev

awk print only lines between two patterns removing first match

From Dev

awk: print lines that DO NOT match patterns in a file, looking at a specific column

From Dev

awk if find pattern skip n lines

From Dev

Awk print $0 print all lines of my files to the last position of column ;how I can print at first?

From Dev

awk lines not match

From Dev

awk match lines that has a "(" but no ")"

From Dev

Perl: Find a match, remove the same lines, and to get the last field

From Dev

Awk: Print last N columns, where N is passed through a variable

From Dev

awk - How to print the number of characters for the first n lines in a file?

From Dev

awk - How to print the number of characters for the first n lines in a file?

From Dev

how to grep and print the next N lines after the hit?

From Dev

awk to print from last match of start match pattern till end pattern

From Dev

awk to print from last match of start match pattern till end pattern

From Dev

Print last N characters from all lines in a file using cut

Related Related

  1. 1

    awk find the last match and print the next N lines

  2. 2

    awk match last record and print

  3. 3

    sed script to print n lines after the last occurence of a match

  4. 4

    awk: print lines after match to end of file

  5. 5

    awk exact variable match, print remaining lines

  6. 6

    awk print 2 lines back if match

  7. 7

    Print the first and last match of a field with awk

  8. 8

    Awk command to print all the lines except the last three lines

  9. 9

    Awk command to print all the lines except the last three lines

  10. 10

    ed print the next n lines as succinctly as possible

  11. 11

    awk - delete line if next lines don't match pattern

  12. 12

    Print lines from match until end of file with awk

  13. 13

    awk compare 2 files, print match and nonmatch lines

  14. 14

    awk + print lines from the first line until match word

  15. 15

    awk compare 2 files, print match and nonmatch lines

  16. 16

    awk print only lines between two patterns removing first match

  17. 17

    awk: print lines that DO NOT match patterns in a file, looking at a specific column

  18. 18

    awk if find pattern skip n lines

  19. 19

    Awk print $0 print all lines of my files to the last position of column ;how I can print at first?

  20. 20

    awk lines not match

  21. 21

    awk match lines that has a "(" but no ")"

  22. 22

    Perl: Find a match, remove the same lines, and to get the last field

  23. 23

    Awk: Print last N columns, where N is passed through a variable

  24. 24

    awk - How to print the number of characters for the first n lines in a file?

  25. 25

    awk - How to print the number of characters for the first n lines in a file?

  26. 26

    how to grep and print the next N lines after the hit?

  27. 27

    awk to print from last match of start match pattern till end pattern

  28. 28

    awk to print from last match of start match pattern till end pattern

  29. 29

    Print last N characters from all lines in a file using cut

HotTag

Archive