grep 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/grep.rb +120 -0
- metadata +45 -0
data/lib/grep.rb
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
#
|
2
|
+
# Skeleton module for the 'grep' routine.
|
3
|
+
#
|
4
|
+
# Ideally, one would do this in their code to import the "grep" call
|
5
|
+
# directly into their current namespace:
|
6
|
+
#
|
7
|
+
# require 'grep'
|
8
|
+
# include Grep
|
9
|
+
# # do something with grep()
|
10
|
+
#
|
11
|
+
#
|
12
|
+
# It is recommended that you look at the documentation for the grep()
|
13
|
+
# call directly for specific usage.
|
14
|
+
#
|
15
|
+
#--
|
16
|
+
#
|
17
|
+
# The compilation of software known as grep.rb is distributed under the
|
18
|
+
# following terms:
|
19
|
+
# Copyright (C) 2005-2006 Erik Hollensbe. All rights reserved.
|
20
|
+
#
|
21
|
+
# Redistribution and use in source form, with or without
|
22
|
+
# modification, are permitted provided that the following conditions
|
23
|
+
# are met:
|
24
|
+
# 1. Redistributions of source code must retain the above copyright
|
25
|
+
# notice, this list of conditions and the following disclaimer.
|
26
|
+
#
|
27
|
+
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
28
|
+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
29
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
30
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
|
31
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
32
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
33
|
+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
34
|
+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
35
|
+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
36
|
+
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
37
|
+
# SUCH DAMAGE.
|
38
|
+
#
|
39
|
+
#++
|
40
|
+
|
41
|
+
module Grep
|
42
|
+
|
43
|
+
#
|
44
|
+
# Grep works like a shell grep. `file' can be either a string,
|
45
|
+
# containing the name of a file to load and handle, or an IO object
|
46
|
+
# (such as $stdin) to deal with. `pattern' can be either a string or
|
47
|
+
# Regexp object which contains a pattern. Patterns as strings treat
|
48
|
+
# no part of the string as `special', such as '.' or '?' in a
|
49
|
+
# regex. `pre_context' and `post_context' determine the amount of
|
50
|
+
# lines to return that came before or after the content that was
|
51
|
+
# matched, respectively. If there are overlaps in the context, no
|
52
|
+
# duplicates will be printed.
|
53
|
+
#
|
54
|
+
|
55
|
+
def grep(file, pattern, pre_context=0, post_context=0)
|
56
|
+
if file.kind_of? String
|
57
|
+
file = File.new(file, "r")
|
58
|
+
end
|
59
|
+
|
60
|
+
if ! file.kind_of? IO
|
61
|
+
throw IOError.new("File must be the name of an existing file or IO object")
|
62
|
+
end
|
63
|
+
|
64
|
+
if pattern.kind_of? String
|
65
|
+
pattern = /#{Regexp.escape(pattern)}/
|
66
|
+
end
|
67
|
+
|
68
|
+
if ! pattern.kind_of? Regexp
|
69
|
+
throw StandardError.new("Pattern must be string or regexp")
|
70
|
+
end
|
71
|
+
|
72
|
+
cache = []
|
73
|
+
lines = []
|
74
|
+
|
75
|
+
loop do
|
76
|
+
begin
|
77
|
+
line = file.readline
|
78
|
+
cache.shift unless cache.length < pre_context
|
79
|
+
cache.push(line)
|
80
|
+
|
81
|
+
if line =~ pattern
|
82
|
+
lines += cache
|
83
|
+
cache = []
|
84
|
+
if post_context > 0
|
85
|
+
post_context.times do
|
86
|
+
begin
|
87
|
+
lines.push(file.readline)
|
88
|
+
rescue IOError => e
|
89
|
+
break
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
rescue IOError => e
|
95
|
+
break
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
file.each_line do |line|
|
101
|
+
cache.shift unless cache.length < pre_context
|
102
|
+
cache.push(line)
|
103
|
+
|
104
|
+
if line =~ pattern
|
105
|
+
lines += cache
|
106
|
+
if post_context > 0
|
107
|
+
post_context.times do
|
108
|
+
begin
|
109
|
+
lines.push(file.readline)
|
110
|
+
rescue Exception => e
|
111
|
+
break
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
return lines
|
119
|
+
end
|
120
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: grep
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2006-04-09 00:00:00 -07:00
|
8
|
+
summary: Module to emulate the 'grep' utility from a unix system
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: erik@hollensbe.org
|
12
|
+
homepage:
|
13
|
+
rubyforge_project: shell-tools
|
14
|
+
description:
|
15
|
+
autorequire: grep
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
authors:
|
29
|
+
- Erik Hollensbe
|
30
|
+
files:
|
31
|
+
- lib/grep.rb
|
32
|
+
test_files: []
|
33
|
+
|
34
|
+
rdoc_options: []
|
35
|
+
|
36
|
+
extra_rdoc_files: []
|
37
|
+
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
requirements: []
|
43
|
+
|
44
|
+
dependencies: []
|
45
|
+
|