Created
November 22, 2008 04:32
-
-
Save miketierney/27743 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -w | |
require 'optparse' | |
require 'open-uri' | |
class PowerGrep | |
def initialize | |
@results => "Initialized!" | |
end | |
attr_accessor :results, :line_number, :number_of_lines, :file_type, :pattern, :quiet_response | |
def self.open_file(file) | |
File.foreach(file) do |line| | |
begin | |
parse_input(file, line) | |
rescue Exception | |
@results += "#{file}: No such file or directory." | |
end | |
end | |
end | |
def self.parse_input(item, line) | |
@line_number += 1 | |
if line =~ /#{@pattern}/ | |
@number_of_lines += 1 | |
#@results += "#{item}: #{((line.chomp).gsub(/<\/?[^>]*>/, "")).lstrip}\n" # TODO: Write this better. Either create a method that sanitizes these things, or pull it out somehow. | |
@results += "#{item}: #{scrub(line)}\n" # TODO: Write this better. Either create a method that sanitizes these things, or pull it out somehow. | |
@results.empty? ? @quiet_response = "false" : @quiet_response = "true" | |
@fast_results << @line_number | |
end | |
end | |
def self.scrub line | |
clean_line = line.chomp.lstrip.rstrip | |
end | |
def self.run commands | |
@results = "" | |
@line_number = 0 | |
@number_of_lines = 0 | |
@file_type = "file" | |
@pattern = "" | |
@fast_results = [] | |
@quiet_response = "false" | |
options = {} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: power_grep.rb [OPTIONS] [PATTERN] [FILE/URLs]" | |
opts.separator "Example: ruby power_grep.rb -i error /usr/lib/errors.log" | |
opts.separator "" | |
opts.on("-f", "--fast-search", "Fast Search") do |f| | |
options[:fast] = f | |
end | |
opts.on("-i", "--ignore-case", "Case insensitive search") do |i| | |
options[:ignorecase] = i | |
end | |
opts.on("-q", "--quiet", "Quiet search @results.") do |q| | |
options[:quiet] = q | |
end | |
opts.on_tail("-h", "--help", "Show this message") do | |
puts opts | |
exit | |
end | |
end.parse! | |
if options[:fast] | |
options[:ignorecase] ? @pattern = Regexp.new(Regexp.escape(ARGV.shift), 1) : @pattern = Regexp.new(Regexp.escape(ARGV.shift)) | |
elsif options[:ignorecase] | |
@pattern = Regexp.new(ARGV.shift, 1) | |
else | |
@pattern = Regexp.new(ARGV.shift) | |
end | |
ARGV.each do |item| | |
if item =~ /^(ht|f)tp(s?)/ | |
@line_number = 0 | |
open(item).each_line do |line| | |
parse_input(item, line) | |
end | |
else | |
@line_number = 0 | |
open_file item | |
end | |
options[:fast] ? @results = @fast_results.join(", ") : @results | |
options[:quiet] ? @results = @quiet_response : @results | |
end | |
#DEBUGGING | |
# puts "---------" | |
# p options | |
# p options[:quiet] | |
# p ARGV | |
# puts "---------" | |
# p @pattern | |
# puts "---------" | |
# p @results | |
#puts @results | |
# puts "---------" | |
# puts "There are #{@number_of_lines} matches for this query." | |
puts @results | |
end | |
end | |
PowerGrep.run(ARGV) if $0 == __FILE__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment