Module: Grep

Defined in:
lib/grep.rb

Overview

Skeleton module for the ‘grep’ routine.

Ideally, one would do this in their code to import the “grep” call directly into their current namespace:

require 'grep'
include Grep
# do something with grep()

It is recommended that you look at the documentation for the grep() call directly for specific usage.

The compilation of software known as grep.rb is distributed under the following terms: Copyright © 2005-2006 Erik Hollensbe. All rights reserved.

Redistribution and use in source form, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright

notice, this list of conditions and the following disclaimer.

THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

++

Instance Method Summary collapse

Instance Method Details

#grep(file, pattern, pre_context = 0, post_context = 0) ⇒ Object

Grep works like a shell grep. ‘file’ can be either a string, containing the name of a file to load and handle, or an IO object (such as $stdin) to deal with. ‘pattern’ can be either a string or Regexp object which contains a pattern. Patterns as strings treat no part of the string as ‘special’, such as ‘.’ or ‘?’ in a regex. ‘pre_context’ and ‘post_context’ determine the amount of lines to return that came before or after the content that was matched, respectively. If there are overlaps in the context, no duplicates will be printed.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/grep.rb', line 55

def grep(file, pattern, pre_context=0, post_context=0)
  if file.kind_of? String
    file = File.new(file, "r")
  end

  if ! file.kind_of? IO
    throw IOError.new("File must be the name of an existing file or IO object")
  end

  if pattern.kind_of? String
    pattern = /#{Regexp.escape(pattern)}/
  end

  if ! pattern.kind_of? Regexp
    throw StandardError.new("Pattern must be string or regexp")
  end

  cache = []
  lines = []

  loop do
    begin
      line = file.readline
      cache.shift unless cache.length < pre_context
      cache.push(line)
      
      if line =~ pattern
        lines += cache
        cache = []
        if post_context > 0
          post_context.times do
            begin
              lines.push(file.readline) 
            rescue IOError => e
              break
            end
          end
        end
      end
    rescue IOError => e
      break
    end
  end
  

  file.each_line do |line|
    cache.shift unless cache.length < pre_context
    cache.push(line)

    if line =~ pattern
      lines += cache
      if post_context > 0
        post_context.times do
          begin
            lines.push(file.readline) 
          rescue Exception => e
            break
          end
        end
      end
    end
  end

  return lines
end