Class: Grepper
- Inherits:
-
Object
- Object
- Grepper
- Defined in:
- lib/grepper.rb,
lib/formatter.rb
Overview
Grepper – simple way to grep through files and save results
Description
The Grepper class greps through files, and returns a result set of Grepper::Result objects. Each Result object represents the matches found in a single file. The Result contains a set of Grepper::Match objects. Each Match object represents a line (the line that matched) and before- and after-context arrays. If no before or after lines were requested, those context values will be nil.
To use, you prepare a Grepper object; call run on it; and walk through the result set. This distribution comes with the Grepper::Formatter class, which is built on top of Grepper and provides fairly canonical-looking output by walking through Grepper objects. (See lib/formatter.rb.) Meanwhile, here are the details.
Preparing a Grepper object
You instantiate Grepper, and provide it with:
-
a pattern, in the form of a regular expression
-
zero or more options, as an array
-
an array of filenames
Setting the grepper’s pattern
To specify a pattern for the Grepper object:
grepper.pattern = /abc/
Setting the grepper’s file list
grepper.files = ["file1.txt", "file2.txt"]
grepper.files = Dir["grep_me/*.txt"]
Options
To set options, you provide an array of strings. For example, for a reverse grep with two lines of after-context you would do:
grepper. = %w{ v A2 }
Available options are:
-
v– negate the sense of the match -
An– provide n lines of after-context -
Bn– provide b lines of before-context
Generating the result set
To generate the result set, you call run.
grepper.run
Now you can access grepper.results, and walk through all the matches.
Walking through the result set and match set
Given a Grepper object grepper, the result set will be at grepper.results. It will iterate per entry, yielding the filename and the corresponding set of matches.
grepper.results.each do |filename, matches|
...
end
For each match set, you can iterate like this:
matches.each do |lineno, before, line, after|
...
end
If you want to walk through either set and just get the objects, not broken out into their properties, you can do:
grepper.results.each_result do |result|
and
matches.each_match do |match|
Version
0.9.1, December 23, 2008
Author
David A. Black ([email protected])
Copyright and license
Copyright © 2008, Ruby Power and Light, LLC
Released under the Ruby license. No warranty of any kind. Use entirely at your own risk.
Defined Under Namespace
Modules: MatchesExtension, ResultsExtension Classes: Formatter, Match, Result
Constant Summary collapse
- VERSION =
'0.9.1'
Instance Attribute Summary collapse
-
#files ⇒ Object
Returns the value of attribute files.
-
#options ⇒ Object
Returns the value of attribute options.
-
#pattern ⇒ Object
Returns the value of attribute pattern.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Instance Method Summary collapse
-
#initialize ⇒ Grepper
constructor
A new instance of Grepper.
-
#run ⇒ Object
Generate the result set.
Constructor Details
#initialize ⇒ Grepper
Returns a new instance of Grepper.
107 108 109 110 111 112 |
# File 'lib/grepper.rb', line 107 def initialize @files = [] @options = [] @results = [].extend(ResultsExtension) @negate = false end |
Instance Attribute Details
#files ⇒ Object
Returns the value of attribute files.
104 105 106 |
# File 'lib/grepper.rb', line 104 def files @files end |
#options ⇒ Object
Returns the value of attribute options.
104 105 106 |
# File 'lib/grepper.rb', line 104 def @options end |
#pattern ⇒ Object
Returns the value of attribute pattern.
104 105 106 |
# File 'lib/grepper.rb', line 104 def pattern @pattern end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
105 106 107 |
# File 'lib/grepper.rb', line 105 def results @results end |
Instance Method Details
#run ⇒ Object
Generate the result set.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/grepper.rb', line 141 def run @negate = true if .include?('v') after_count = $1.to_i if .find {|o| /A(\d)/.match(o) } before_count = $1.to_i if .find {|o| /B(\d)/.match(o) } files.each do |file| result = Result.new(file) results << result buffer = [] File.open(file) do |fh| i = 0 while line = fh.gets i += 1 if match?(line) match = Match.new(line) match.lineno = i if after_count match.after = get_after_context(fh, after_count) i += match.after.to_s.scan(/\n/).size end if before_count match.before = buffer.empty? ? nil : buffer buffer = [] end result.matches << match else if before_count buffer << line buffer.shift if buffer.size > before_count end end end end end end |