Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An example:</p> <pre><code>def match_environment( filename, check_content = 'test', no_of_lines = 4) line_pattern = "%1s %5i: %s%" File.open(filename, "r:windows-1251") do |f| puts "====File #{f.path}====" #Contains previous 4 lines extract = [] hit = 0 #flag to print next lines f.each_with_index do |line,linno| linno += 1 #Start line counting with 1 extract.shift if extract.size &gt;= no_of_lines #remove last line if line.include? ( check_content ) #print previous 4 lines extract.each_with_index{|pline, index| puts line_pattern % [ nil, linno - extract.size + index, pline.chomp ] } extract = [] #avoid puts line_pattern % [ '!', linno, line.chomp ] #This line has a hit hit = no_of_lines #print next four line else if hit &gt; 0 #put next lines puts line_pattern % [ nil, linno,line.chomp ] hit -= 1 puts ' [...]' if hit == 0 end extract &lt;&lt; line #add actual line end end end #close file end #match_environment #Test if we find test anywhere match_environment( __FILE__, 'test' ) exit #remove to check directory #loop on directory Dir['*'].each do |filename| next unless File.file? filename match_environment( filename ) end </code></pre> <p><code>extract</code> is an Array with the last n lines (parameter <code>no_of_lines</code>). Each new line is appended, previous lines are removed. <code>extract</code> is a storage of the previous n lines.</p> <p><code>hit</code> is a counter to print the next n lines.</p> <p>One advantage of this solution: If you match your search pattern (in my example it is <code>'test'</code>) multiple times, the four lines before and after are only counted from first and last hit.</p> <p><code>line_pattern</code> is a pattern for the output (flag if the line contains the search string, 5 characters for the line number, followed by the line).</p> <p>If I execute the script, I get:</p> <pre><code>====File test.rb==== 1: % 2: % ! 3: def match_environment( filename, check_content = 'test', no_of_lines = 4)% 4: line_pattern = "%1s %5i: %s%"% 5: File.open(filename, "r:windows-1251") do |f|% 6: puts "====File #{f.path}===="% 7: #Contains previous 4 lines% [...] 33: end #match_environment% 34: % 35: % ! 36: #Test if we find test anywhere% ! 37: match_environment( __FILE__, 'test' )% 38: % 39: exit #remove to check directory% 40: #loop on directory% 41: Dir['*'].each do |filename|% [...] </code></pre> <p>Remark: <code>match_environment( __FILE__, 'test' )</code> executes the script on the script file itself.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload