4723

Unix help to extract/print 50 lines after every 3rd occurrence pattern till end of file

Question:

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

Answer1:

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 <h3>Explanation</h3>

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

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

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 <h3>Test</h3> $ 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

Recommend

  • RegEx match two or more same character non-consecutive
  • Javascript - Regular expressions, what to do now that its all true
  • show input value in error message using jquery validation
  • How to search by variable in awk
  • How to export SSL certificate from azure to local server?
  • .NET Obfuscation [SmartAssembly]
  • Append dynamic vue component after specific element
  • ASP Net Core - Mixing External Identity Provider with Individual User Accounts for Audit Tracking
  • Does Perl currently (5.8 and 5.10) make any promises about the order alternations will be used?
  • Setting UIToolbar background image with iOS5.1
  • Change color of row programmatically in WatchKit
  • Unable to use dot layout (graphviz as a library)
  • overlapping appointments using the entity framework
  • Remove characters after a specific character in column
  • Is an if-let or a normal if condition better?
  • Cannot connect to native local socket on android 5.1
  • Wait for .each() .getJSON request to finish before executing a callback
  • Stitching 2 images (OpenCV)
  • Error while using PDF Kit
  • matching similar elements in between two lists
  • Loading fixtures in sails tests
  • Glassfish - java.lang.NoClassDefFoundError
  • Is it possible to run clang with llc flags
  • Clear activity stack before launching another activity
  • Possible to get mouse events fired when cursor is outside page?
  • Best way to dynamically load an xml configuration file into a Flex4/Flash movie at runtime?
  • Get specific string
  • JQuery Internet Explorer and ajaxstop
  • Atlas images wrong size on iPad iOS 9
  • Checking free space on FTP server
  • How to delay loading a property with linq to sql external mapping?
  • Is there a javascript serializer for JSON.Net?
  • Cannot connect to cassandra from Spark
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Where to put my custom functions in Wordpress?
  • ActionScript 2 vs ActionScript 3 performance
  • Transpose CSV data with awk (pivot transformation)
  • Buffer size for converting unsigned long to string
  • Binding checkboxes to object values in AngularJs
  • java string with new operator and a literal